Resizing JPG / JPEG

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Resizing JPG / JPEG

Post by Ambush Commander »

Are there any functions in PHP used for resizing JPG / JPEG images (I found some that adjusted image quality, but not for resizing? How about for GIF?

And if there isn't, are there any good ways to generate thumbnails? (Supposedly JPEG has embedded thumbnails)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would recommend you take a look at the GD libraries found here [php_man]gd[/php_man]
User avatar
emperor
Forum Newbie
Posts: 16
Joined: Tue Mar 02, 2004 11:20 am
Location: UK

Post by emperor »

GD can be slightly complicated to get used to, but the most common way to resize JPEGs using it is like this

Code: Select all

<?php
    $src = ImageCreateFromJPEG ($src_filename);

    $width = ImageSx ($src);
    $height = ImageSy ($src);
    $x = $new_width;
    $y = $new_height;

    $dst = ImageCreateTrueColor ($x, $y);
    ImageCopyResampled ($dst, $src, 0, 0, 0, 0, $x, $y, $width, $height);
    ImageJPEG ($dst, $dst_filename);    // or ImagePNG
    DestroyImage ($dst);
?>
Like Phenom says though, it's best to read up on GD anyway so you know exactly what the functions are doing.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Okay, thanks. Why did they name them GD? Doesn't sound like pictures at all... (don't answer that question, I think it's like Graphic _)

Now the only problem I have is installing the GD libraries on my system...
Hur. I still haven't gotten Mysql to work either,
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Ambush Commander wrote:Okay, thanks. Why did they name them GD? Doesn't sound like pictures at all... (don't answer that question, I think it's like Graphic _)
What does "gd" stand for?
In gd 1.0, it stood for "gif draw." After the Unisys patent on the LZW compression used in GIF came to light and GIF support was dropped, it did not officially stand for anything, but let's just say "graphics draw" and leave it at that. (GIF support is back, thanks to the expiration of the patent, but gd can draw much more than GIFs.)
Post Reply