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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.