Jump to content

[SOLVED] Need help with simple cropping in php . . .


Styles2304

Recommended Posts

I'm trying to crop the image: http://l.yimg.com/us.yimg.com/i/us/nws/weather/gr/33n.png down to 177 by 120. I would just do this in photoshop but the image changes (it's not always 33n.png) but always has the same dimensions and is always a png with transparency. I've tried a few of the functions but either couldn't get them to work or they just didn't function as promised. I've also searched these forums and the php manual and this just isn't something I can figure out on my own.

 

If it's a simple script, I would VERY much appreciate it if someone could write it out for me with just a few notes so that I can learn.

 

Thank You,

Styles

Link to comment
Share on other sites

I just came across some code on php.net that works perfectly as I need it to. Here it is..

 

<?php
// Max height and width
$max_width = 100;
$max_height = 100;

// Path to your jpeg

$upfile '/path/to/file.jpg';
    Header("Content-type: image/jpeg");
   
    $size = GetImageSize($upfile); // Read the size
          $width = $size[0];
          $height = $size[1];
         
          // Proportionally resize the image to the
          // max sizes specified above
         
          $x_ratio = $max_width / $width;
          $y_ratio = $max_height / $height;

          if( ($width <= $max_width) && ($height <= $max_height) )
          {
               $tn_width = $width;
               $tn_height = $height;
          }
          elseif (($x_ratio * $height) < $max_height)
          {
               $tn_height = ceil($x_ratio * $height);
               $tn_width = $max_width;
          }
          else
          {
               $tn_width = ceil($y_ratio * $width);
               $tn_height = $max_height;
          }
     // Increase memory limit to support larger files
    
     ini_set('memory_limit', '32M');
    
     // Create the new image!
     $src = ImageCreateFromJpeg($upfile);
     $dst = ImageCreateTrueColor($tn_width, $tn_height);
     ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
     ImageJpeg($dst);
// Destroy the images
ImageDestroy($src);
ImageDestroy($dst);
?>
mail at soylentgreens dot com
30-Mar-2005 06:37
How about this for cropping images...

<?php

$imgfile = "img.jpg";
$cropStartX = 300;
$cropStartY = 250;
$cropW   = 200;
$cropH   = 200;

// Create two images
$origimg = imagecreatefromjpeg($imgfile);
$cropimg = imagecreatetruecolor($cropW,$cropH);

// Get the original size
list($width, $height) = getimagesize($imgfile);

// Crop
imagecopyresized($cropimg, $origimg, 0, 0, $cropStartX, $cropStartY, $width, $height, $width, $height);

// TODO: write code to save new image
// or, just display it like this:
header("Content-type: image/jpeg");
imagejpeg($cropimg);

// destroy the images
imagedestroy($cropimg);
imagedestroy($origimg);

?>

 

You'd just need to modify some of the values to do what you need to do. It works perfectly for me

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.