darknessmdk Posted August 24, 2007 Share Posted August 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66584-solved-resizingsaving-an-image-upload/ Share on other sites More sharing options...
Fadion Posted August 24, 2007 Share Posted August 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66584-solved-resizingsaving-an-image-upload/#findComment-333564 Share on other sites More sharing options...
darknessmdk Posted August 27, 2007 Author Share Posted August 27, 2007 It works, but it renames the file as -upload and saves it as a binary file. I replaced $fname[1] with ".jpg" and it saves it as a jpg Quote Link to comment https://forums.phpfreaks.com/topic/66584-solved-resizingsaving-an-image-upload/#findComment-335241 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.