cs.punk Posted June 16, 2009 Share Posted June 16, 2009 What exact functions do I need to resize a uploaded .jpeg image to a 320x200 size? Though theres a tricky part, I want them to resize while keeping the aspect ratio (I have attached a image example)... Any ideas? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/162435-solved-resize-jpeg-image-to-320x200-size/ Share on other sites More sharing options...
akitchin Posted June 16, 2009 Share Posted June 16, 2009 please avoid double-posting your topics. to resize an image, you'll need to use some of the GD functions. keeping the same aspect ratio is a simple calculation, and here's an example tutorial: http://www.phptoys.com/e107_plugins/content/content.php?content.46 there's a plethora of these tutorials in google, i'd suggest looking around. another option is using a third-party plugin such as phpThumb, which is extremely handy and fairly quick to setup. Quote Link to comment https://forums.phpfreaks.com/topic/162435-solved-resize-jpeg-image-to-320x200-size/#findComment-857415 Share on other sites More sharing options...
rhodesa Posted June 16, 2009 Share Posted June 16, 2009 http://us2.php.net/imagecopyresampled The new dimensions though you will have to calculate. The easiest way is to take your aspect ratio: 320 / 200 = 1.6 Then on the new image, if it's aspect ratio is greater then 1.6 you will want to resize the height and crop the width. Less then 1.6, resize the width and crop the height. aka: Starting Image: 600 x 300 600 / 300 = 2.0 ~~~> Greater then 1.6, so height resize, width crop 300 / 200 * 600 = 400 ~~~> Resized Height (resized width is 200) Resized Image: 400 x 200 Crop Image to 320 x 200 Quote Link to comment https://forums.phpfreaks.com/topic/162435-solved-resize-jpeg-image-to-320x200-size/#findComment-857420 Share on other sites More sharing options...
cs.punk Posted June 17, 2009 Author Share Posted June 17, 2009 please avoid double-posting your topics. to resize an image, you'll need to use some of the GD functions. keeping the same aspect ratio is a simple calculation, and here's an example tutorial: http://www.phptoys.com/e107_plugins/content/content.php?content.46 there's a plethora of these tutorials in google, i'd suggest looking around. another option is using a third-party plugin such as phpThumb, which is extremely handy and fairly quick to setup. Double posting? Uhm I posted a topic of this in the CSS board, but since it became a PHP query, I posted it in the PHP board?.. Thanks guys, will check the two links! Quote Link to comment https://forums.phpfreaks.com/topic/162435-solved-resize-jpeg-image-to-320x200-size/#findComment-857826 Share on other sites More sharing options...
rhodesa Posted June 17, 2009 Share Posted June 17, 2009 Double posting? Uhm I posted a topic of this in the CSS board, but since it became a PHP query, I posted it in the PHP board?.. no worries. next time though, just ask to have it moved. we have the power to do that Quote Link to comment https://forums.phpfreaks.com/topic/162435-solved-resize-jpeg-image-to-320x200-size/#findComment-857885 Share on other sites More sharing options...
dreamwest Posted June 17, 2009 Share Posted June 17, 2009 Creates perfect resized images: class SimpleImage { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } //******************settings below*****************// // resize $image = new SimpleImage(); $image->load(pic.jpg); $image->resize(140,110); //wxh $image->save(pics/pic.jpg); Quote Link to comment https://forums.phpfreaks.com/topic/162435-solved-resize-jpeg-image-to-320x200-size/#findComment-857896 Share on other sites More sharing options...
cs.punk Posted June 18, 2009 Author Share Posted June 18, 2009 Thank you akitchin, rhodesa and dreamwest! I struggled allot with it but I finally got it lol: If anyone is interested: <?php function image_resize($original_image,$to_width,$to_height) {// Get the original geometry and calculate scales list($width, $height) = getimagesize($original_image); $xscale=$width/$to_width; $yscale=$height/$to_height; // Recalculate new size with default ratio if ($yscale>$xscale) {$new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else {$new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); } global $new_image; $new_image = imagecreatetruecolor($new_width, $new_height); $tmp_image = imagecreatefromjpeg ($original_image); imagecopyresampled($new_image, $tmp_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); } ?> $file_tmp = $_FILES[pic][tmp_name]; if (is_uploaded_file($file_tmp)) {image_resize($file_tmp, 320, 260); imagejpeg($new_image, "../file_uploads/userimage.jpeg"); } Quote Link to comment https://forums.phpfreaks.com/topic/162435-solved-resize-jpeg-image-to-320x200-size/#findComment-858875 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.