SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reducing the size of an image

    Hi,

    Does anyone know if PHP can compress an image. I have a site where by users can upload images of themselves to be displayed in their profile. But I want to know if it is possible to compress it. Ie if they upload a 400kb photo, can I reduce this at all?

    Kjc
    Eternity

  2. #2
    SitePoint Addict whofarted's Avatar
    Join Date
    Aug 2001
    Location
    lost, If you find me please return me to St.Louis
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use GD or image magick & make it thumbnail all images over a certain size.

    That would help make sure all images are say...less than 500x500 so they don't cause huge pictures to need scroll bars & file size (weight) would be reduced at the same time.
    You smell something?

  3. #3
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the image functions, you can resize, crop, and otherwise manipulate the image. You can also pass a second paramater to imagejpeg() to specify the jpeg compression quality (if you want to use jpegs).

    Regards,

    Matt.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kjc
    Hi,

    Does anyone know if PHP can compress an image. I have a site where by users can upload images of themselves to be displayed in their profile. But I want to know if it is possible to compress it. Ie if they upload a 400kb photo, can I reduce this at all?

    Kjc
    This is part of a script that I found here [img]images/smilies/smile.gif[/img]
    PHP Code:
    function resize($image$thumb$x$y$type) {
    $rat 150;  // the max length (or width)
    $success 0;
    if(
    $type == "jpg" ) {
    $src ImageCreateFromJpeg($image);
    }
    elseif(
    $type == "gif" ) {
    $src ImageCreateFromGif($image);
    }
    if (
    $x $y) {
    $imagey ceil(($rat $y) / $x);
    $imagex $rat;
    }
    if (
    $y $x) {
    $imagex ceil(($rat $x) / $y);
    $imagey $rat;
    }
    if (
    $y == $x) {
    $imagex $rat;
    $imagey $rat;
    }
    $dst_img=ImageCreateTrueColor($imagex,$imagey); 
    ImageCopyResized($dst_img,$src,0,0,0,0,$imagex,$imagey,$x,$y);
    if(
    $type == "jpg" ) {
    ImageJpeg($dst_img$thumb);
    $success 1;
    chmod($thumb0644);
    }
    elseif(
    $type == "gif" ) {
    ImageGif($dst_img$thumb);
    $success 1;
    }
    return 
    $success;

    I hope it helps [img]images/smilies/wink.gif[/img]
    the neigbours (free) WIFI makes it just a little more fun

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here is a thumbnail script I created ... you use it by simply passing the filepath and desired width to it in an <img> tag ... e.g. ...
    Code:
    <img src="thumbnail.php?src=/path/to/image.jpg&maxw=400" />
    http://www.siteguru.co.uk/codes/thumbnail.txt
    Ian Anderson
    www.siteguru.co.uk

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    canada
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Peanuts, your fuunction would be useful to me, but I have a question. When I run the function on an image the exif data is lost. Is it possible to create the new images and keep the exif data?

  7. #7
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by alt-f4
    Peanuts, your fuunction would be useful to me, but I have a question. When I run the function on an image the exif data is lost. Is it possible to create the new images and keep the exif data?
    Sure you just have to point the exif function to the correct path (even if it's a temp path..) or just fill a session var with the information ... you could even put the info in a database before you use the function... anything is possible
    the neigbours (free) WIFI makes it just a little more fun

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    canada
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess sometimes I just need to know if it is possible.
    That gives me one more thign to do thanks a lot
    Scott

  9. #9
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your replies. I will try it out in the next fews day and get back to you with the results.

    Cheers
    Eternity

  10. #10
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are there any solutions which don't require GD to be installed. My web host doesn't have GD...
    Eternity

  11. #11
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what about image magick? by the way... are there any discussions about IM at the moment?


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
  •