creating thumbnails

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

creating thumbnails

Post by psychotomus »

how can I create thumbnails for .jpg and .png files?
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

Code: Select all

$destimg = imagecreatetruecolor ($newWidth, $newHeight) 
    or die ("Problem In Creating image"); // Create thumbnail canvas

// Use one or use conditional to select i.e. if JPEG
$srcimg = imagecreatefromjpeg ("path to full sized") 
    or die ("Problem In opening Source Image");  // For JPEG

// else PNG
$srcimg = imagecreatefrompng ("path to full sized") 
    or die ("Problem In opening Source Image");  // For PNG

imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height)
     or die ("Problem In resizing");  // Resample the image from $srcimg to the $destimg

// Use one or use conditional to select i.e. if JPEG
imagejpeg ($destimg, "path to thumb") or die("Problem In saving");  // Save JPG

// else PNG
imagepng ($destimg, "path to thumb") or die("Problem In saving");  // Save PNG
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

I got this and its not saving the file to the server.

Code: Select all

<?php
$destimg = imagecreatetruecolor (250, 250) 
    or die ("Problem In Creating image"); // Create thumbnail canvas 

// Use one or use conditional to select i.e. if JPEG 
$srcimg = imagecreatefromjpeg ("uploads/boo.jpg") 
    or die ("Problem In opening Source Image");  // For JPEG 



imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, 250, 250, 494, 733) 
     or die ("Problem In resizing");  // Resample the image from $srcimg to the $destimg 

// Use one or use conditional to select i.e. if JPEG 
imagejpeg ($destimg, "uploads/tester.jpg") or die("Problem In saving");  // Save JPG 

?>
[/url]
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

is your upload folder chmod 0777?

put it this way, it works for me on mine http://www.saxonian.co.uk/imagehost/ , also try full paths so /home/you/public_html/ .... /uploads/ thats what i use.



Edit: found your problem you should use relative paths properly :D don't forget the ./ before the path, either that or use full paths.

Code: Select all

<?php
$destimg = imagecreatetruecolor (250, 250)
    or die ("Problem In Creating image"); // Create thumbnail canvas

// Use one or use conditional to select i.e. if JPEG
$srcimg = imagecreatefromjpeg ("./uploads/boo.jpg")
    or die ("Problem In opening Source Image");  // For JPEG



imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, 250, 250, 494, 733)
     or die ("Problem In resizing");  // Resample the image from $srcimg to the $destimg

// Use one or use conditional to select i.e. if JPEG
imagejpeg ($destimg, "./uploads/tester.jpg") or die("Problem In saving");  // Save JPG

?>
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

how would i get the width and height from $srcimg??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

Code: Select all

list ($width, $height) = getimagesize("./uploads/boo.jpg");
// $width file width
// $height file height
and your not thumbnailing them to a square are you? ie distorting images and generally making them look horrible :P
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

yes... what should i thumbnail them to?
mparker1113
Forum Commoner
Posts: 28
Joined: Wed Apr 05, 2006 9:39 am

I would like to catch and resize photos from file input stat

Post by mparker1113 »

from file input status, before i move them to a different directory using moveuploadedfile() or other means.

I think that the photos directly from the camera are to big to be moved, and would like to resize them immediately, so that I can move them.

would the code for this be similar to the posted, using imagecreatefromjpeg, and makejpeg() ?

Would I be able to get this to work, (without storing the file first)?

Thanks for input.

Mike
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

psychotomus wrote:yes... what should i thumbnail them to?
As always: The beauty lies in the eye of the beholder ;) 100x100 perhaps?
If you really want square shaped thumbnails, you'll have to resize them to a certain size, which is somewhat bigger than the final size. You can then crop them by using ImageCopy();

aerodromoi
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

I get this error when trying to resize an image after uploading it.

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mounted-storage/home16a/sub002/sc18478-RGIJ/www/admin_wallpapers.php on line 196
Problem In resizing

Code: Select all

$loc = $row['id'] + 1; 
          
         if (move_uploaded_file($_FILES["file"]["tmp_name"], "./wallpapers/" .  $loc . '.jpg')) 
         { 


            //SAVE THUMBNAIL 
            $srcimg = imagecreatefromjpeg ("./wallpapers/" .  $loc . '.jpg') 
               or die ("Problem In opening Source Image");  // For JPEG 

            imagecopyresampled ($destimg,  $srcimg, 0, 0, 0, 0, 150, 150, imagesx($srcimg) , imagesy($srcimg)) 
                or die ("Problem In resizing");  // Resample the image from $srcimg to the $destimg 
             
            imagejpeg ($destimg, "./wallpapers_thumbs/" . $loc . ".jpg") or die("Problem In saving");  // Save JPG
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

$phpNut wrote:

Code: Select all

list ($width, $height) = getimagesize("./uploads/boo.jpg");
// $width file width
// $height file height
and your not thumbnailing them to a square are you? ie distorting images and generally making them look horrible :P
Nice code. First time I've seen "list" used well.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

jayshields, im trying to get help. don't hijack my thread about complimenting other coders. I know there good, thats why I ask them for help =)
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Re: creating thumbnails

Post by xpgeek »

psychotomus wrote:how can I create thumbnails for .jpg and .png files?
use http://phpthumb.sourceforge.net/.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

my code is fine. its almost exactly how coppermine is done but without a class.

since im using HTTP POST to get the filename, how can i replace this and set $srcimg to the $_FILES['file'] which is allready in memory instead of loading the file and using up twice as much memory. maybe I can get it to work that way.


$srcimg = imagecreatefromjpeg ("./wallpapers/" . $loc . '.jpg') or die ("Problem In opening Source Image"); // For JPEG
Post Reply