Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading Graphic??

Status
Not open for further replies.

bluedollar

Programmer
Jul 24, 2003
174
GB
I am having problems succesfully uploading a graphic.

I am using a php script to manipulate the size/quality of the graphic.

Basically the problem is:

1) When the pic is uploaded to the server it is given a *.tmp format
2) When the uploaded pic path is passed to the script that manipulates the pic it fails because *.tmp is not a recognised picture format.

The relevant code is shown below.

Any help would be greatly appreciated.

Thanks

Dan

=============================================================

I am using the following code to get the path of a graphic:

<tr><th bgcolor = #999999 align="right"> Picture Upload: </th>
<td><input type="file" value="" name="graphic"> </td></tr>
</table><P><table>

=============================================================

When the user submits the above the following script is then called:

function save($tran) {
global $id;
global $graphic;

#Connects to mysql Database
include_once("Cconnection.php");
$obconnect = new connect_class();
$obconnect->connection();
$obconnect->dbconnect();

#Checks to see if there and calls a function that calls the graphic manipulation function
if ($graphic != "") {
$file_name = $obconnect->picuploader($graphic,"/apache/htdocs/uploads/social","social","graphic",250);
$SQL = "update social set graphic = '$file_name' where social_id = '$id'";
$obconnect->execute($SQL,'3');
}


$obconnect->closelink();

}

=============================================================

This function is called from the code above and uses the graphic manipulation class shown afterwards

function picuploader($src,$dest,$table,$name,$size) {

global $HTTP_POST_FILES;

foreach($HTTP_POST_FILES as $file_name => $file_array) {


#$src_name = $src;
//$src_name .= "/";
$src_name = $file_array['name'];
echo''.$src.'';

$last = $this->lastid($name,$table);

$file_name = $dest;
$file_name .= "/";

if ($last != "") {
$ex = explode(".",$last);
settype( $num, integer);
$num = $ex[0];
$num++;
$file_name .= $num;
$file_name .= ".jpg";
settype( $name, string);
$name = $num;
$name .= ".jpg";

}
else {
$file_name .= "1.jpg";
$name = "1.jpg";
}

include_once("cthumbnail.php");
$thumb=new thumbnail("$src_name");
$thumb->size_width(100); // set width for thumbnail, or
$thumb->size_height(300); // set height for thumbnail, or
$thumb->size_auto($size); // set the biggest width or height for thumbnail
$thumb->jpeg_quality(75); // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
$thumb->save("$file_name");
//$thumb->save("\\uploads/new.jpg");


#echo'<br>'.$name.'<br>';

return $name;
}
}

==================================================================

The full code for the graphic manipulation class is shown below:

<?
##############################################
# Shiege Iseng Resize Class
# 11 March 2003
# shiegege@yahoo.com
# ################
# Thanks to :
# Dian Suryandari <dianhau@yahoo.com>
/*############################################
Sample :
$thumb=new thumbnail("./shiegege.jpg"); // generate image_file, set filename to resize
$thumb->size_width(100); // set width for thumbnail, or
$thumb->size_height(300); // set height for thumbnail, or
$thumb->size_auto(200); // set the biggest width or height for thumbnail
$thumb->jpeg_quality(75); // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
$thumb->show(); // show your thumbnail
$thumb->save("./huhu.jpg"); // save your thumbnail to file
----------------------------------------------
Note :
- GD must Enabled
- Autodetect file extension (.jpg/jpeg, .png, .gif, .wbmp)
but some server can't generate .gif / .wbmp file types
- If your GD not support 'ImageCreateTrueColor' function,
change one line from 'ImageCreateTrueColor' to 'ImageCreate'
(the position in 'show' and 'save' function)
*/############################################


class thumbnail
{
var $img;

function thumbnail($imgfile)
{
//detect image format
$this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
$this->img["format"]=strtoupper($this->img["format"]);
if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
//JPEG
$this->img["format"]="JPEG";
$this->img["src"] = ImageCreateFromJPEG ($imgfile);
} elseif ($this->img["format"]=="PNG") {
//PNG
$this->img["format"]="PNG";
$this->img["src"] = ImageCreateFromPNG ($imgfile);
} elseif ($this->img["format"]=="GIF") {
//GIF
$this->img["format"]="GIF";
$this->img["src"] = ImageCreateFromGIF ($imgfile);
} elseif ($this->img["format"]=="WBMP") {
//WBMP
$this->img["format"]="WBMP";
$this->img["src"] = ImageCreateFromWBMP ($imgfile);
} else {
//DEFAULT
echo "Not Supported File";
exit();
}
@$this->img["lebar"] = imagesx($this->img["src"]);
@$this->img["tinggi"] = imagesy($this->img["src"]);
//default quality jpeg
$this->img["quality"]=75;
}

function size_height($size=100)
{
//height
$this->img["tinggi_thumb"]=$size;
@$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
}

function size_width($size=100)
{
//width
$this->img["lebar_thumb"]=$size;
@$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
}

function size_auto($size=100)
{
//size
if ($this->img["lebar"]>=$this->img["tinggi"]) {
$this->img["lebar_thumb"]=$size;
@$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
} else {
$this->img["tinggi_thumb"]=$size;
@$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
}
}

function jpeg_quality($quality=75)
{
//jpeg quality
$this->img["quality"]=$quality;
}

function show()
{
//show thumb
@Header("Content-Type: image/".$this->img["format"]);

/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
$this->img["des"] = ImageCreate($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
@imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
//JPEG
imageJPEG($this->img["des"],"",$this->img["quality"]);
} elseif ($this->img["format"]=="PNG") {
//PNG
imagePNG($this->img["des"]);
} elseif ($this->img["format"]=="GIF") {
//GIF
imageGIF($this->img["des"]);
} elseif ($this->img["format"]=="WBMP") {
//WBMP
imageWBMP($this->img["des"]);
}
}

function save($save="")
{
//save thumb
if (empty($save)) $save=strtolower("./thumb.".$this->img["format"]);
/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
$this->img["des"] = ImageCreate($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
@imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
//JPEG
imageJPEG($this->img["des"],"$save",$this->img["quality"]);
} elseif ($this->img["format"]=="PNG") {
//PNG
imagePNG($this->img["des"],"$save");
} elseif ($this->img["format"]=="GIF") {
//GIF
imageGIF($this->img["des"],"$save");
} elseif ($this->img["format"]=="WBMP") {
//WBMP
imageWBMP($this->img["des"],"$save");
}
}
}
?>
 
Solved it, I had to modify the picupload function, updated code shown below (takes the file extension from uploaded file and uses it to rename the tmp file):

function picuploader($misc,$dest,$table,$name,$size) {

global $HTTP_POST_FILES;

foreach($HTTP_POST_FILES as $file_name => $file_array) {


$file_name = $file_array['name'];
$temp2 = explode (".", $file_name);


//$src_name .= "/";
$src_name = $file_array['tmp_name'];
$temp = explode(".", $src_name);
$src_name = $temp[0];
$src_name .= ".";
$src_name .= $temp2[1];

rename($file_array['tmp_name'], $src_name);

$last = $this->lastid($name,$table);

$file_name = $dest;
$file_name .= "/";

if ($last != "") {
$ex = explode(".",$last);
settype( $num, integer);
$num = $ex[0];
$num++;
$file_name .= $num;
$file_name .= ".jpg";
settype( $name, string);
$name = $num;
$name .= ".jpg";

}
else {
$file_name .= "1.jpg";
$name = "1.jpg";
}

include_once("cthumbnail.php");
$thumb=new thumbnail("$src_name");
$thumb->size_width(100); // set width for thumbnail, or
$thumb->size_height(300); // set height for thumbnail, or
$thumb->size_auto($size); // set the biggest width or height for thumbnail
$thumb->jpeg_quality(75); // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
$thumb->save("$file_name");
//$thumb->save("\\uploads/new.jpg");


#echo'<br>'.$name.'<br>';

return $name;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top