SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cut and resize image

    In server i have some images with random dimension.

    I want cut the image to render width=height.

    ex.

    image with width=400 and height=800
    i want cut the image to render height=400

    After this operation i want resize the image to 64x64


    How i can to do it?
    Thanks

  2. #2
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using PHP's GD Library:
    http://au3.php.net/gd
    PHP | MySQL | (X)HTML | CSS

  3. #3
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First create a new image.
    Then crop the current image and copy it to the new image.

    PHP Code:
    //output content header
    header("Content-type: image/gif");
    //create a new true color image with 64px width and height
    $newImage imagecreatetruecolor(6464);
    //put the original image into memory as a variable
    $oldImage imagecreatefromgif("imagefilename.gif");
    //crop the original image at 400 by 400
    //copy the cropped part to a 64 by 64 part onto the $newImage
    imagecopyresampled($newImage$oldImage00006464400400);
    //output the new image by type
    imagegif($newImage); 
    That does it for both the cropping and resizing.
    I hope the code is right. I didn't test it. Just typed quickly.
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  4. #4
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You will need to have PHP GD Support on your server.
    To check whether GD is enabled, you can execute phpinfo(); to see your extensions.
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.
    But what is the function for the cut?

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,438
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    Doing exactly as you asked using ImageMagick; to work on landscape photos you would need to change the code. But this will prove the principle.
    PHP Code:
    <?php

    $size 
    getimagesize('upload.jpg');

    exec("/usr/local/bin/convert upload.jpg -crop {$size[0]}x{$size[0]}+0+0 -resize 64x64 new_image.jpg");

    ?>
    Note:
    There is a difference between Contrids and my code: Contrids code is doing as you asked cropping to 400 then resizing to 64x64. My code is cropping the height to match the width then resizing to 64x64. You could modify Contrids code to have the same effect as mine or mine to match his.
    Last edited by Rubble; Mar 24, 2007 at 07:05. Reason: Added note

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your not familiar with programming, you could download the images to your computer and resize them with a pretty easy program called "Easy Thumbnailer".

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure if you already solved your image resizing issue, but for you and all other devs like you looking for a way to transform images on the fly, you might want to check the open-source project Asido: there are a lot of useful features (http://www.asido.info/about/features/), including various types of image resize, watermarking, image copy, cropping, etc. It can use various platforms: not only GD but ImageMagick too (either via shell or via any of the php extensions like php_magickwand.dll or php_imagick.dll). Here's the list of drivers: http://www.asido.info/about/drivers/


Bookmarks

Posting Permissions

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