Forum Moderators: coopster

Message Too Old, No Replies

Image Resize on Upload

         

inveni0

4:14 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



I have a script to upload an image to the server so that I can easily replace photos on my website. However, I'd like to even skip the process of having to resize the photo in Photoshop by having the script also resize the image that is uploaded.

Does anyone know how to do this?

dreamcatcher

4:30 pm on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check out the PHP Scripts Repository, they have a few classes that will enable you to resize images:

[phpclasses.org...]

dc

inveni0

5:01 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Well, the thing is, I don't need a big fancy script.

All I need is a line to execute after the file is successfully uploaded that sizes the uploaded file to 400x300.

inveni0

5:45 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Okay, here's my upload script ->

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
$target_path = "";

$target_path = $target_path . $filename;

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "You have successfully added a photo for Item # ".$row_Upload['Room'];
} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}
?>

Where should I place code that will resize the image?

inveni0

9:20 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Is there a simple mogrify command I can place somewhere in that script? I'm totally at a loss. Every rescource I've found gives no help when trying to do the upload and resize in one step.

dmmh

10:29 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



its quite easy tbh, I do it like this:

1. upload image and echo message
2. IF upload was succesfull, I call a function that resizes the image in the upload dir and inside the same function, the original is deleted


<?
function func_makethumb_avatar_upload($uid){
//describe dynamic image resize function for uploading user avatars and delete original
ob_start(); //output buffering
$thumb_quality = 80; // JPEG image quality (0-100) for thumbnails
$thumb_width = 80; // resized images will have this width
$req_dir = getcwd();// get current working directory


chdir('/home/public_html/');//this is just here so there is no need for those pesky relative URLs ( ../../ ), set it to your base directory, the directory your html files are in
$avatar_url = "user/avatars/$uid.jpg";//path to image


if (file_exists($avatar_url)){
list($width, $height, $type, $attr) = getimagesize($avatar_url); //...get its size and stuff like that and...
if ($width!= $thumb_width){ //...if the thumbnail is not the same size as $thumb_width we need to resize it so...
$image_handle = imagecreatefromjpeg($avatar_url); //
$thumb_height = round(($thumb_width / $width) * $height); //resizing etc
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height) or $thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image_handle, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); //resample the original onto $thumbnail (which is a blank image)
unlink("user/avatars/$uid.jpg");//now delete the original
imagejpeg($thumbnail, $avatar_url, $thumb_quality); //actually create a jpg from $thumbnail, store it in location $avatar_url, with quality $thumb_quality
imagedestroy($image_handle); //clear buffer #*$!
imagedestroy($thumbnail);
ob_end_flush();//end output buffering
}
}
chdir($req_dir); //change back to original working directory
}//end function?>

you can extend the function even further, with 2 more variables you can pass it the path and image name so you dont have to hardcode it into the function; maximum flexibility ;-)

[edited by: dmmh at 10:35 pm (utc) on Jan. 4, 2006]

[edited by: tedster at 11:11 pm (utc) on Jan. 4, 2006]
[edit reason] fix side scroll [/edit]

inveni0

10:34 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



So would I insert that:

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

--> HERE <--

echo "You have successfully added a photo for Item # ".$row_Upload['Room'];
} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}


Or somewhere else in the code?

dmmh

10:37 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



correctemundo ;)

inveni0

3:25 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Okay, so now my code is -

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
$target_path = "";

$target_path = $target_path . $filename;

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

function func_makethumb_avatar_upload($uid){
//describe dynamic image resize function for uploading user avatars and delete original
ob_start(); //output buffering
$thumb_quality = 60; // JPEG image quality (0-100) for thumbnails
$thumb_width = 400; // resized images will have this width
$req_dir = getcwd();// get current working directory

$avatar_url = $filename;//path to image

if (file_exists($avatar_url)){
list($width, $height, $type, $attr) = getimagesize($avatar_url); //...get its size and stuff like that and...
if ($width!= $thumb_width){ //...if the thumbnail is not the same size as $thumb_width we need to resize it so...
$image_handle = imagecreatefromjpeg($avatar_url); //
$thumb_height = round(($thumb_width / $width) * $height); //resizing etc
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height) or $thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image_handle, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); //resample the original onto $thumbnail (which is a blank image)
unlink("user/avatars/$uid.jpg");//now delete the original
imagejpeg($thumbnail, $avatar_url, $thumb_quality); //actually create a jpg from $thumbnail, store it in location $avatar_url, with quality $thumb_quality
imagedestroy($image_handle); //clear buffer #*$!
imagedestroy($thumbnail);
ob_end_flush();//end output buffering
}
}

}

echo "You have successfully added a photo for Item # ".$row_Upload['Room']. ". You have stored ".$filename;

} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}
?>

<center>
<p><font color=red>
<?=$_REQUEST[message]?>
</font> </p>
<p>&nbsp;</p>
<p><br>
You are currently editing Item # <?php echo $row_Upload['Room'];?></p>
<p>&nbsp;
</p>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
<p>
<input name="filename" type="radio" value="<?php echo $row_Upload['Photo1'];?>">
Photo1
<br>
<input name="filename" type="radio" value="<?php echo $row_Upload['Photo2'];?>">
Photo2 <br>
<input name="filename" type="radio" value="<?php echo $row_Upload['Photo3'];?>">
Photo3 <br>
<label> </label>
</p>
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload:
<input name="uploadedfile" type="file" />
<br />
<input type="submit" value="Upload File" />
</p>
</form>

<p>&nbsp;</p>
<p><a href="roomadmin.php">RETURN TO ADMIN</a></p>
<p>&nbsp; </p>
</center>

<HEAD>

<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

<?php
mysql_free_result($Upload);
?>

But this doesn't work...it uploads the image, but there is no resize. I have verified that GD is on the server...what it the problem?

dmmh

4:42 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



because you are approaching this the wrong way.
A function is basically a predefined set of actions, which can be re-usable. Because a function is re-usable, you are better of putting it in a seperate file, say function.php, which is included (include('path_to_file/functions.php')) on the page the function is called.

Your biggest flaw is you are indeed using the function, but you are merely defining it, not calling it (executing it). Generally, you define the functions in a seperate page and call them when you need them.

one defines a function like:
function func_name($var1){
//lots of code here
}

you call that same function (execute it) by doing:

func_name($array_of_whatever);

so, you need to do it like this:

$target_path = $target_path . $filename;

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
func_make_thumb_avatar_upload($target_path);
}

thats it, then you echo the rest of the page

adjust the function to the stuff below and put it in a external file:

function func_makethumb_avatar_upload($filename){
//describe dynamic image resize function for uploading user avatars and delete original
ob_start(); //output buffering
$thumb_quality = 60; // JPEG image quality (0-100) for thumbnails
$thumb_width = 400; // resized images will have this width
$req_dir = getcwd();// get current working directory

$avatar_url = $filename;//path to image

if (file_exists($avatar_url)){
list($width, $height, $type, $attr) = getimagesize($avatar_url); //...get its size and stuff like that and...
if ($width!= $thumb_width){ //...if the thumbnail is not the same size as $thumb_width we need to resize it so...
$image_handle = imagecreatefromjpeg($avatar_url); //
$thumb_height = round(($thumb_width / $width) * $height); //resizing etc
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height) or $thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image_handle, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); //resample the original onto $thumbnail (which is a blank image)
unlink($avatar_url);//now delete the original
imagejpeg($thumbnail, $avatar_url, $thumb_quality); //actually create a jpg from $thumbnail, store it in location $avatar_url, with quality $thumb_quality
imagedestroy($image_handle); //clear buffer #*$!
imagedestroy($thumbnail);
ob_end_flush();//end output buffering
}
}

inveni0

6:04 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Is there a way to do the resize without a function? I only need it done after each upload (one upload per pageload) and never after that, so creating a function that can be called more than once seems like more than I need.

Is there code I can add that just does the resize to $filename?

I have tried this:
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $filename)) {
list($width, $hieght) = getimagesize($filename);
imagecopyresampled($filename, $filename, 0, 0, 0, 0, 400, 300, $width, $height);
echo "You have successfully added a photo for Item # ".$row_Upload['Room']. ". You have stored ".$filename;

} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}

The file is successfully uploaded, but the resize doesn't work.

inveni0

6:59 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Okay, I took a new PHP file as a function, included it and it works. BUT...does GD have a size restriction on the image? I can't resize large images (approx. 1MB, JPG, straight from the digital camera).

dmmh

9:49 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $filename)) {
$thumb_quality = 80;
$thumb_width = 80;
list($width, $height, $type, $attr) = getimagesize($filename); //...get its size and stuff like that and...
if ($width!= $max_thumb_width){ //...if the thumbnail is not the same size as $thumb_width we need to resize it so...
$image_handle = imagecreatefromjpeg($filename); //
$thumb_height = round(($thumb_width / $width) * $height); //resizing etc
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height) or $thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image_handle, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); //resample the original onto $thumbnail (which is a blank image)
unlink($filename);//now delete the original
imagejpeg($thumbnail, $filename, $thumb_quality); //actually create a jpg from $thumbnail, store it in location $avatar_url, with quality $thumb_quality
imagedestroy($image_handle); //clear buffer #*$!
imagedestroy($thumbnail);
ob_end_flush();//end output buffering
echo "You have successfully added a photo for Item # ".$row_Upload['Room']. ". You have stored ".$filename;

} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}

typo dude, u used 'hieght' instead of 'height'

but your better of with the code I posted above

seriously, you need to start thinking scalability...scalability ;-)
but hey, whatever rocks your boat

inveni0

9:53 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Actually, the code I used is simpler and works very well. The size restriction is an effect of how my hosting server handles too much load.

I got an email from my server administrator with the explanation just before I came on here to say the problem is as solved as it can be.

dmmh

9:57 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



okay :)
at least you got it working :p

inveni0

10:02 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Yep. And I did wind up using a function, so your help with that was appreciated. Thanks!

dmmh

10:07 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



welcome :)

and one day you will acknowledge the power of functions, when you have to update a few hundred PHP scripts, instead of 1 function
always best to learn the hard way :D