ionik Posted July 28, 2008 Share Posted July 28, 2008 Ok, Well I cant use imagemagik so lets already out of the question or i wouldnt be posting this, what I've been trying to accomplish is cropping out the middle of a image into a new 90x60 image and cannot seem to get anything working. Anyone know of a good function/class/snippet that does this? Quote Link to comment https://forums.phpfreaks.com/topic/116997-php-gd-crop-images/ Share on other sites More sharing options...
obsidian Posted July 28, 2008 Share Posted July 28, 2008 There are a couple approaches, depending on the effect you are after. The first is to simply crop the center 90x60 region, no matter the size of the original image (this is what I will attempt to communicate below), but the second would be to crop the 90x60 ratio from the center of an image as large as the image is (only cropping off the extra space), then resizing down to 90x60. I'm not sure which better fits your needs. To crop a 90x60 region, though, try something like this: <?php // assuming that $img holds the image with which you are working $img_width = imagesx($img); $img_height = imagesy($img); // New image size $width = 90; $height = 60; // Starting point of crop $tlx = floor($img_width / 2) - floor ($width / 2); $tly = floor($img_height / 2) - floor($height / 2); // Adjust crop size if the image is too small if ($tlx < 0) { $tlx = 0; } if ($tly < 0) { $tly = 0; } if (($img_width - $tlx) < $width) { $width = $img_width - $tlx; } if (($img_height - $tly) < $height) { $height = $img_height - $tly; } $im = imagecreatetruecolor($width, $height); imagecopy($im, $img, 0, 0, $tlx, $tly, $width, $height); // Then output or save the $img file ?> Hope this at least helps to give you some direction. Quote Link to comment https://forums.phpfreaks.com/topic/116997-php-gd-crop-images/#findComment-601733 Share on other sites More sharing options...
bernardmoes Posted February 23, 2010 Share Posted February 23, 2010 maybe this works: <?php 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/116997-php-gd-crop-images/#findComment-1016700 Share on other sites More sharing options...
salathe Posted February 23, 2010 Share Posted February 23, 2010 maybe this works: Considering that class does no cropping of the image whatsoever, whether it works or not is irrelevant. I would advise against copy/pasting code in answer to questions unless you know it will help. Quote Link to comment https://forums.phpfreaks.com/topic/116997-php-gd-crop-images/#findComment-1016726 Share on other sites More sharing options...
bernardmoes Posted February 23, 2010 Share Posted February 23, 2010 you can modify it so it will crop Quote Link to comment https://forums.phpfreaks.com/topic/116997-php-gd-crop-images/#findComment-1016730 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.