joecooper Posted July 23, 2009 Share Posted July 23, 2009 Hi. i am using the following code to resize an image and display it to the user. <?php // File and new size $filename = 'test.jpg'; $percent = 0.5; // Content type header('Content-type: image/jpeg'); // Get new sizes list($width, $height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent; // Load $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); // Resize imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Output imagejpeg($thumb); ?> the problem with this is it looses quality when it resizes. round edges become a bit squared and you can notice defects. the original file is 1600x1200 and 1.8MB, then after it resizes to say 1024x768, the filesize is <300kb is there any other code i could use that will resize with good quality Link to comment https://forums.phpfreaks.com/topic/167169-solved-image-resize/ Share on other sites More sharing options...
Bricktop Posted July 23, 2009 Share Posted July 23, 2009 Hi Joe, Try using imagecopyresampled (http://uk2.php.net/imagecopyresampled) Something like: <?php // File and new size $filename = 'test.jpg'; $percent = 0.5; // Content type header('Content-type: image/jpeg'); // Get new sizes list($width, $height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent; // Load $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); // Resize imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Output imagejpeg($thumb); ?> Link to comment https://forums.phpfreaks.com/topic/167169-solved-image-resize/#findComment-881432 Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 Try using imagecopyresampled (http://uk2.php.net/imagecopyresampled) Something like: <?php // File and new size $filename = 'test.jpg'; $percent = 0.5; // Content type header('Content-type: image/jpeg'); // Get new sizes list($width, $height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent; // Load $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); // Resize imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Output imagejpeg($thumb); ?> ... did you not just paste back to him EXACTLY what he put in his first post? Link to comment https://forums.phpfreaks.com/topic/167169-solved-image-resize/#findComment-881434 Share on other sites More sharing options...
Bricktop Posted July 23, 2009 Share Posted July 23, 2009 lol no, check the line below //Resize for a subtle change of code! Link to comment https://forums.phpfreaks.com/topic/167169-solved-image-resize/#findComment-881435 Share on other sites More sharing options...
joecooper Posted July 23, 2009 Author Share Posted July 23, 2009 imagecopyresampled is what i used instead of imagecopyresized and it works fine! Link to comment https://forums.phpfreaks.com/topic/167169-solved-image-resize/#findComment-881444 Share on other sites More sharing options...
Bricktop Posted July 23, 2009 Share Posted July 23, 2009 Great! Glad it worked for you Joe. Link to comment https://forums.phpfreaks.com/topic/167169-solved-image-resize/#findComment-881445 Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 lol no, check the line below //Resize for a subtle change of code! my mistake, sorry! Link to comment https://forums.phpfreaks.com/topic/167169-solved-image-resize/#findComment-881459 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.