SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Oct 15, 2001, 13:28 #1
- 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 ?
-
Oct 15, 2001, 13:37 #2
- 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, $src, 0, 0, 0, 0, $nDWidth, $nDHeight, $arSrcSize[0], $arSrcSize[1] );
ImageJPEG( $dst, $sDest );
}
else
{
echo "Error!";
}
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 13:39.
-
Oct 15, 2001, 14:32 #3
- 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 14:35.
-
Oct 15, 2001, 15:04 #4
- 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.
-
Oct 16, 2001, 09:16 #5
- 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 withPHP Code:phpinfo();
If you don't have it and can't get it ... then as freddy suggested try other alternatives ...
Bookmarks