Jump to content

PHP GD Crop Images


ionik

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 year later...

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;   
   }      
}
?>

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.