Making thumbnails in PHP

Discussion in 'PHP' started by cancer10, Oct 6, 2006.

  1. #1
    Hello,

    I want to convert a 1024 x 786 wallpaper into thumbnail in PHP

    Wots the code for it?


    Thanx in advance
     
    cancer10, Oct 6, 2006 IP
  2. Vizuke

    Vizuke Peon

    Messages:
    339
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can just use basic HTML tags.

    <img src=wallpaper.png width=320 height=240 alt="wallpaper" />
     
    Vizuke, Oct 7, 2006 IP
  3. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I dnt want to shrink the images.
     
    cancer10, Oct 7, 2006 IP
  4. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
  5. Cotillion

    Cotillion Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You evidently need to use GD library:

    www.php.net/manual/en/ref.image.php
     
    Cotillion, Oct 7, 2006 IP
  6. JRJR

    JRJR Active Member

    Messages:
    172
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    What makes the page quicker?
    1- using php, creating a html tag with calculated width and height;
    2- creating a new image, using imagecopyresized?

    Thanks in advance.
     
    JRJR, Oct 7, 2006 IP
  7. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #7
    "resize" a image by adding width/height tags to image is pretty noobish. it does not shrink image, the browser just squeezes the full size image small, causing extrem long loading times and poor thumbnail quality.

    watch and learn:
    
    <?php
      //hail to the king, babe! [URL="http://www.shareyourride.net"]http://www.shareyourride.net[/URL] :)
    
      //define the datatype for the browser
      header("Content-type:image/jpeg");
    
     //find your picture 
     $path = $_GET['path'];
    
      //load picture
      $pic = imagecreatefromjpeg($path);
    
      //get size
      $x = imagesx($pic);
      $y = imagesy($pic);
    
      //create a new image with the given size. this here will scale it down it to
      //10% (original width * 0.1, original height * 0.1)
      $pic_small = imagecreatetruecolor($x * 0.1, $y * 0.1);
      imagecopyresampled($pic_small, $pic, 0, 0, 0, 0, $x * 0.1, $y * 0.1, $x, $y);
    
      //send jpeg back to browser
      imagejpeg($pic_small);
    
      //destroy all evidence
      imagedestroy($pic);
      imagedestroy($pic_small);
    ?>
    
    Code (markup):
    save this as a new file "resize.php", it must be in the same folder as your gallery.php.
    this is the code for your gallery site to display the thumbnail:

    
    <img src="resize.php?path=test.jpg">
    
    HTML:
    thats all. test.jpg is the name of the image you want to resize here.
     
    falcondriver, Oct 7, 2006 IP
  8. thedark

    thedark Well-Known Member

    Messages:
    1,346
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    168
    Digital Goods:
    1
    #8
    the best option is to make a script to make a thumbnail for the image when it is uploading. name it with the same filename like the original image, but with _tn at the end, or something like this, and you will have 2 images, original and thumbnail to work with.
     
    thedark, Oct 7, 2006 IP
  9. lazydog

    lazydog Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You might also want to take a look at Imagemagick - http://www.imagemagick.org/
     
    lazydog, Oct 7, 2006 IP
  10. JRJR

    JRJR Active Member

    Messages:
    172
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #10
    Thank you very much falcondriver.
    I've changed the way I show images in the articles of my WW2 website. I think it's much faster than before. :)
     
    JRJR, Oct 8, 2006 IP
  11. disgust

    disgust Guest

    Messages:
    2,417
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    0
    #11
    you've hit on a decent point, actually.

    creating the image does take up resources on the server, especially if it's a high traffic site and you're doing this over and over again. in that case, if possible, you'd want to cache the images that're created to save some resources.
     
    disgust, Oct 8, 2006 IP
  12. Gekkie

    Gekkie Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    What makes the page quicker?
    1- using php, creating a html tag with calculated width and height;
    2- creating a new image, using imagecopyresized?

    Best is to create thumbs when you upload the images and load does.
     
    Gekkie, Oct 9, 2006 IP
  13. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #13
    Barti1987, Oct 9, 2006 IP
  14. JRJR

    JRJR Active Member

    Messages:
    172
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #14
    When I wrote my last post, I wasn't aware of the GIF problem. So I had to change the code to work 100% with any type of image (at least, jpg, gif and png...I only use these three types).

    Now...I notice the website is faster than without this resize issue.

    Thank you all. :D
     
    JRJR, Oct 11, 2006 IP
  15. tr0nic

    tr0nic Active Member

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #15

    I have used the same thing - works wonderful!
     
    tr0nic, Oct 11, 2006 IP
  16. pondlife

    pondlife Peon

    Messages:
    898
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #16
    pondlife, Oct 12, 2006 IP