Jump to content

[SOLVED] Resizing/saving an image upload


darknessmdk

Recommended Posts

I've been trying to figure this out for a bit now.

 

I found this on the forum... but it doesnt work.

 

should I use fopen to save the file?

 

$uploadedfile = "xmotivate.jpg";


$src = imagecreatefromjpeg($uploadedfile);

// get size of image
list($width,$height)=getimagesize($uploadedfile);

if ($width > $height)
{
$target_width=110;
$ratio = $target_width/$width;
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
}
else
{
$target_height = 110;
$ratio =  $target_height/$height;
$newwidth = $width * $ratio;
$newheight = $height * $ratio;

$tmp=imagecreatetruecolor($newwidth,$newheight);
}
// resize 
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);


// save resized
$filename = "user.jpg"; $_FILES['userfile']['name'];
imagejpeg($tmp,$filename,100); 

imagedestroy($src);
imagedestroy($tmp);

 

I get these errors

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/content/L/e/g/Legacy007/html/tests/image_manip/image2.php on line 27

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/content/L/e/g/Legacy007/html/tests/image_manip/image2.php on line 32

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/content/L/e/g/Legacy007/html/tests/image_manip/image2.php on line 35

Link to comment
https://forums.phpfreaks.com/topic/66584-solved-resizingsaving-an-image-upload/
Share on other sites

I think i got your problem. Ure getting an error because a wrong parameter is passed to those functions, and thats because u put "$tmp=imagecreatetruecolor($newwidth,$newheight);" between the if-else part. Just put it out of the if - else and it should work:

 

else
{
$target_height = 110;
$ratio =  $target_height/$height;
$newwidth = $width * $ratio;
$newheight = $height * $ratio;

//$tmp=imagecreatetruecolor($newwidth,$newheight); (it was here)
}

$tmp=imagecreatetruecolor($newwidth,$newheight); (should be here)

 

As u have the code, the imagecreatetruecolor() will work only if $width < $height.

 

Also about the filename. This makes no sense:

$filename = "user.jpg"; $_FILES['userfile']['name'];

 

U can use smth like:

$fname = explode('.', $_FILES['userfile']['name']);
$filename = $fname[0] . "-uploaded" . "." . $fname[1];

 

So u have the original filename and extension plus an '-uploaded' in the middle. Hope it works.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.