SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamically resize pictures

    If I have a picture on my server named "pic.jpg" thats 640x480 px.
    How can then fix so that when I call that picture typing "pic-200.jpg" it will resize itself so that the height will be 200 px?

  2. #2
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <img src='http://www.yoursite.com/resize.php?image=pic.jpg&width=200' />

    In resize.php:
    use header() to output the image rather than saving it to the file.

    This is Image generation on the fly.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the image is used at this size often, it might be good to check to see if this image size exists and if it doesn't, generate and save. Then next time this size is requested it just redirects to the existing image, instead of having to process it every time.

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,443
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    There is a neat way of doing this with mod-rewrite; I can not remember if this is the tutorial I tried out before: http://sneak.co.nz/projects/img-resizing/

    Couple of other ways;
    GD - a watermarking ex ample but you get the idea
    PHP Code:
    <?php 
    $image 
    imagecreatefromjpeg'image.jpg' ); 
    imagettftext$image9016172, -1'Arial.ttf''Some text' ); 
    imagettftext$image90161105, -1'Arial.ttf''Some more text' ); 
    header"Content-Type: image/png" ); 
    imagepng$image ); 
    imagedestroy$image ); 
    ?>
    Imagemagick ( a bit more complicated code as it is showing some more options of how to use. )
    PHP Code:
    <?php
    $photo
    ="../code/sunflower.jpg";
    $THUMB_SZ  125;
    $THUMB_PRESZ  $THUMB_SZ 2;
    $QUALITY 87;
    $cmd "convert -size $THUMB_PRESZ".'x'."$THUMB_PRESZ \"$photo\"" .
    " -thumbnail $THUMB_SZ".'x'."$THUMB_SZ.
    " -unsharp 0.2x0.6+1.0" .
    " -quality $QUALITY JPG:-";

    header("Content-type: image/jpeg");
    passthru($cmd$retval);
    ?>
    You save the code on a page of its own as something like resize.php and call using <img src="resize.php">
    Last edited by Rubble; Jul 23, 2009 at 08:57. Reason: Added GD example


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
  •