www.webdeveloper.com
Results 1 to 4 of 4

Thread: Crop tall .jpg to square WITHOUT complex cropping add-ons

  1. #1
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265

    Exclamation Crop tall .jpg to square WITHOUT complex cropping add-ons

    I have a handful of jpg files that are tall and vary in size. I want the excess length to be trimmed (so aligned top-center and cropped to a square) but I don't want to add any extra systems like thumb.php, ImageMagick, etc. to the page because simplicity and file-size are important for this quick project.

    They are all 430 pixels wide and they should all be aligned to the top (rather than cropped to the center or something)

    Any advice or help would be greatly appreciated.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    Here's a stripped down version of a function I wrote a few years ago that should work (if I didn't break it):
    PHP Code:
    /**
    * Resize image to specific dimension, cropping as needed
    * @return resource Resized image resource, or boolean false on failure
    * @param string $imgFile Path to image to be resized
    * @param int $width
    * @param int $height
    * @param string $error Error message
    */
    function myCrop($imgFile$width=430$height=430, &$error null)
    {
       
    $attrs = @getimagesize($imgFile);
       if(
    $attrs == false or $attrs[2] != IMG_JPEG)
       {
          
    $error "Uploaded image is not JPEG or is not readable by this page.";
          return 
    false;
       }
       
    $resized imagecreatetruecolor($width$height);
       
    $result imagecopyresampled($resized$src0000$width$height$width$height);
       if(
    $result == false)
       {
          
    $error "Error trying to resize and crop image.";
          return 
    false;
       }
       else
       {
          return 
    $resized;
       }

    Usage would be:
    PHP Code:
    $resizedImage myCrop(imagecreatefromjpeg('path/to/your/image.jpg'));
    // now do whatever you want with the image, such as saving it as a file:
    imagejpeg($resizedImage'path/to/resized/image.jpg'90); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    I'm trying it but it isn't working.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    Looks like I screwed up a couple things when stripping out the features you didn't need. This test worked fine for me. (Note that it's outputting the image directly, versus writing it to a file. See my first reply for the imagejpeg() syntax to write it to a file if that's what you need.)
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'true);

    $imageFile 'C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg';
    $newImage myCrop($imageFile430430$error);
    if(
    $newImage == false) {
        die(
    $error);
    }

    // this is directly displaying the image, change it to write to file if desired:
    header('Content-Type: image/jpeg');
    imagejpeg($newImage);
    exit;

    /**
    * Resize image to specific dimension, cropping as needed
    * @return resource Resized image resource, or boolean false on failure
    * @param string $imgFile Path to image to be resized
    * @param int $width
    * @param int $height
    * @param string $error Error message
    */
    function myCrop($imgFile$width=430$height=430, &$error null)
    {
       
    $attrs = @getimagesize($imgFile);
       if(
    $attrs == false// or $attrs[2] != IMG_JPEG)
       
    {
          
    $error "Uploaded image is not JPEG or is not readable by this page.";
          return 
    false;
       }
       
    $src imagecreatefromjpeg($imgFile);
       if(
    $src == false) {
            
    $error "Unable to load source image.";
            return 
    false;
       }
       
    $resized imagecreatetruecolor($width$height);
       
    $result imagecopyresampled($resized$src0000$width$height$width$height);
       if(
    $result == false)
       {
          
    $error "Error trying to resize and crop image.";
          return 
    false;
       }
       else
       {
          return 
    $resized;
       }
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles