SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict brain's Avatar
    Join Date
    Sep 2001
    Location
    Amsterdam
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create thumbnail from uploaded file

    I want to create a thumbnail from an uploaded file. I already had the following code to make a copy of the original to the thumbs directory:
    copy("/home/users/mysite/pics/".$filename,"/home/users/mysite/thumbs/".$filename);

    Now I want the copied picture (jpeg) to be resized and replaced. The new width must be set to 50, the height must be set automatically (ratio).

    Can anyone give my the code to do that ?

  2. #2
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the code that you need ...
    PHP Code:
    $sSource "/home/users/mysite/pics/$filename";
    $sDest "/home/users/mysite/thumbs/$filename";
    $arSrcSize GetImageSize$sSource );

    $src = @ImageCreateFromJPEG $sSource );

    if (
    $src)
    {
        
    $nDWidth = (int)($arSrcSize[0]/2);
        
    $nDHeight = (int)($arSrcSize[1]/2);
        
    $dst ImageCreateTrueColor ($nDWidth$nDHeight);
        
    ImageCopyResampled$dst$src0000$nDWidth$nDHeight$arSrcSize[0], $arSrcSize[1] );
        
    ImageJPEG$dst$sDest );
    }
    else
    {
        echo 
    "Error!";

    Make sure you have the access to write in the directory ... "../thumbs/"
    Also I have assumed that the image type is JPEG and you want the thumbnail in the same format ...

    For other formats please check the PHP Manual -> Image Functions ...

    Pleae note that you would need the gd library extension in the php to get this code working

    This code resizes the image into half ...
    Please make the resize ratio according to your need ...
    Last edited by Amit; Oct 15, 2001 at 12:39.

  3. #3
    SitePoint Addict brain's Avatar
    Join Date
    Sep 2001
    Location
    Amsterdam
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorry, it didn't work

    This is what happened:
    Fatal error: Call to undefined function: imagecreatetruecolor() in /home/users/mysite/insert.php on line 280

    Isn't there a simpler code ?
    Last edited by brain; Oct 15, 2001 at 13:35.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cause you don't have the GD library installed. If you are on a shared server, its gonna be tough to find a host with everything you need. If you have your own server make sure to compile PHP with GD and JPEG support. Or have a look at other options like Imagemagick, http://www.imagemagick.org again you'll need access to install progs on your server to do any of the above. If you can get ImagMagick running I can give yo uthe code.

    The one thing about ImageMagick over GD is you don't have to recompile PHP to get it working. Ask your host for more details.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess I forgot to mention that this would work with GD Library 2.0 or greater. It seems you have 1.6.2 version of GD ... because ImageCreateTrueColor is in the ver 2.0 or greater.

    Create a new php file with
    PHP Code:
    phpinfo(); 
    as its code and it should tell you a lot about php on your server, here you can find out whether and which version of GD you have ...

    If you don't have it and can't get it ... then as freddy suggested try other alternatives ...


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
  •