SitePoint Sponsor

User Tag List

Page 1 of 6 12345 ... LastLast
Results 1 to 25 of 139
  1. #1
    ********* Articles ArticleBot's Avatar
    Join Date
    Apr 2001
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Article Discussion

    This is an article discussion thread for discussing the SitePoint article, "Simplified Image Resizing with PHP"

  2. #2
    kiscsavo
    SitePoint Community Guest
    didnt work for me that way
    i had to put $wh = imageResize(...)
    and then print_r($wh);// in the same img tag
    then it worked !!!


  3. #3
    Ed
    SitePoint Community Guest
    This is cheating, any idiot can modify the IMG tag width and height attributes. What I was looking for was a PHP script that would actually generate a new image at the requested resolution. Though to be fair you do say this at the bottom. Unfortunately this article came up as the first hit on my google search :-(

  4. #4
    Tiago Santos
    SitePoint Community Guest
    This article was very helpfull for me!

    I am not an expert in PHP so this was really the article that i was looking for.

    Thank you to Greg Knox

  5. #5
    SitePoint Member
    Join Date
    Oct 2004
    Location
    Zurich
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I'm also going to use the 'getimagesize' function - but what I would like is that it only shows the 10 newest images from a folder - is this possible? Or that it shows randomly 10 pictures out of a folder... Appreciate any hints.

  6. #6
    SitePoint Enthusiast craig34's Avatar
    Join Date
    Dec 2003
    Location
    Newton, MA
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A big problem here is that you are loading the entre image, and incurring the bandwidth for it as well, when you could just be loading a thumbnail image that is a fraction of the size. Why load an 80kb image when you only need to load a 5kb image?

  7. #7
    Heidi
    SitePoint Community Guest
    This is for craig34
    YOu are correct however if the image is a hyperlinked thumbnail of the original and you have over 3000 images it is great. Having to resize all images (which takes up server space) and have it load two images...
    This is beter.

    Thank you Greg Knox

    The function can be rewritten for nonpercent values and different value for width and height

  8. #8
    Jack
    SitePoint Community Guest
    What you can do instead of using up bandwidth is have php resize the image before downloading it eg:
    <?php
    // File and new size
    $filename = 'test.jpg';
    $percent = 0.5;

    // Content type
    header('Content-type: image/jpeg');

    // Get new sizes
    list($width, $height) = getimagesize($filename);
    $newwidth = $width * $percent;
    $newheight = $height * $percent;

    // Load
    $thumb = imagecreate($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);

    // Resize
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    // Output
    imagejpeg($thumb);
    ?>

  9. #9
    DatraxMada
    SitePoint Community Guest
    Here is a function which is simpler to call and doesnt mix the PHP and IMG tags.

    <?php
    function imageResize($image, $target, $alt=NULL) {

    //Gets the width and height of the image and outputs it as $theimage[0] (width) and $theimage[1] (height)
    $theimage = getimagesize($image);
    $width = $theimage[0];
    $height = $theimage[1];

    //takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image

    if ($width > $height) {
    $percentage = ($target / $width);
    } else {
    $percentage = ($target / $height);
    }

    //gets the new value and applies the percentage, then rounds the value
    $width = round($width * $percentage);
    $height = round($height * $percentage);

    //Returns the new sizes inside an image tag so you can call it with "imageResize("images/sock001.jpg", "Some alt text", $target)"

    echo "<img src=\"$image\" width=\"$width\" height=\"$height\" alt=\"$alt\">";
    }
    ?>

    <!- Now, you can simply use your imageResize function instead of mixing up your PHP with an IMG tag -->

    <?php imageResize("images/sock001.jpg", 50, "Oh what a beautiful sock"); ?>

  10. #10
    Sunil
    SitePoint Community Guest
    In the above article Posted by: DatraxMada , following

    $width = $theimage;
    $height = $theimage;

    should ble like this ;

    $width = $theimage[0];
    $height = $theimage[1];



  11. #11
    Chris Davidsen
    SitePoint Community Guest
    hi all,

    in DatraxMada code, the following

    Code:
    $width = $theimage;
    $height = $theimage;
    the $theimage is an array, therefore

    the code should be changed to this

    Code:
    $width = $theimage[0];
    $height = $theimage[1];
    Chris Davidsen

  12. #12
    john
    SitePoint Community Guest
    Are you people sure that the code is:

    $width = $theimage;
    $height = $theimage;

    and not:

    $width = $theimage;
    $height = $theimage;

    ???

    I'm seriously lost... help!!!!

  13. #13
    SitePoint Enthusiast badg0003's Avatar
    Join Date
    Aug 2004
    Location
    Ottawa, Ontario
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried the above article out, but found that it was not working that well for me. I've included an updated script that works really well for me. By the way, forgive me by the messiness of the code - I was in a rush! :)

    Code:
    // Max width/height of image.
    $maxImgXY = "125"; 
    
    // Grab image details
    $arrFileSize = getimagesize("filename");
    
    // Check if both sides of image exceed allowable proportions.
    if($arrFileSize[0] < $maxImgXY && $arrFileSize[1] < $maxImgXY) {
        $imgWidth  = $arrFileSize[0];
        $imgHeight = $arrFileSize[1];
    }
    else {
    // Find the largest side (width/height).
    if($arrFileSize[0] > $arrFileSize[1]) {
        $imgLarge = $arrFileSize[0];
        $imgWidth = $maxImgXY;
    
    // Divide that side by the maximum size allowed.
        $aspectRatio = $imgLarge / $maxImgXY;
    
    // Determine size of remaining side (height) using the result above.
        $imgHeight = round($arrFileSize[1] / $aspectRatio);
    }
    else {
        $imgLarge  = $arrFileSize[1];
        $imgHeight = $maxImgXY;
    
    // Divide that side by the maximum size allowed.
        $aspectRatio = $imgLarge / $maxImgXY;
    
    // Determine size of remaining side (width) using the result above.
        $imgWidth = round($arrFileSize[0] / $aspectRatio);
    }
    }

  14. #14
    Joel McDonald
    SitePoint Community Guest
    The problem in the original code is this line:

    <img src="images/sock001.jpg" <?php imageResize($mysock[0],
    $mysock[1], 150); ?>>

    You need to print the results of the function call rather than just call the function. So it would end up looking like this:

    <img src="images/sock001.jpg" <?php print imageResize($mysock[0],
    $mysock[1], 150); ?>>

  15. #15
    Jay Dierda
    SitePoint Community Guest
    Can you explain how you would use this if you were grabbing the images from the images folder and then printing them into a results table?

  16. #16
    Mark
    SitePoint Community Guest
    show us how to resize the image and make it a smaller file size, ya bum! :)

  17. #17
    Chris
    SitePoint Community Guest
    Thanks so much for this! I was looking for ages for a soltion to my problem (how to resize images on the fly to use as thumbnails).

  18. #18
    MDG
    SitePoint Community Guest
    Great

    just what i was looking for :)

  19. #19
    Steve
    SitePoint Community Guest
    Haha. To resize the image and make it a smaller filesize, use a free program like "Irfanview". You can choose to resample the image for better quality, and compress the image at varying percentages to achieve truly miniscule filesizes!

    This article was amazing, thanks! =D

  20. #20
    Imutana
    SitePoint Community Guest
    This isn't code I want to find, it work but it's size is same when i get thumbnail

  21. #21
    SitePoint Member
    Join Date
    Feb 2004
    Location
    leiria
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi there!

    I'm newbie in php.
    I tried to used this function but I receive the following error:

    Warning: getimagesize(image): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in $file on line 30

    Warning: Division by zero in $file on line 10


  22. #22
    pedito
    SitePoint Community Guest
    hi there!

    I'm newbie in php.
    I tried to used this function but I receive the following error:

    Warning: getimagesize(image): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in $file on line 30

    Warning: Division by zero in $file on line 10


  23. #23
    pregardtzs
    SitePoint Community Guest
    call the function with $image not only with image

  24. #24
    james
    SitePoint Community Guest
    the function works well, however my experience has been that if you actually want the returned value (ie - html tag string) to be output to the generated page:

    <img src="images/sock001.jpg" <?php echo imageResize($mysock[0], $mysock[1], 150); ?>>

    you need the "echo".

  25. #25
    james
    SitePoint Community Guest
    also, users of this function should keep in mind that the image is merely resized graphically (ie: force-fit). if the original image is 300kb or 1mb, the "thumbnail" will be just as large in terms of download size.


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
  •