Create thumbnail from jpeg/gif

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
Madsheep
Forum Newbie
Posts: 2
Joined: Mon May 13, 2002 1:04 am
Location: Wuerzburg, Germany

Create thumbnail from jpeg/gif

Post by Madsheep »

Hi,

- first my idea:
I create a page, where users can upload pictures(2) of themselves. The files are saved in a directory "upload". And the upload-site should automatically create a thumbnail of one of the pictures.

- my solution:
I wanted first to call the method "imagecreatefromjpeg" then "imagecreatefromgif". If the file would be valid (*.jpg or *.gif) it should create an img-"object", with which I should create the thumbnail.

- my problem:
the method did nothing. So I added some debug-output. The line before imagecreatefromjpeg was printed. The line after the method not. When I call the method it breaks the function (in which the source is included).

Does anybody know what I did wrong, or does anyone knows an example for this problems.

Many thanks,
Joachim
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Your PHP installation needs to have support for the GD libraries, either compile in PHP if on a *nix platform or if Windows uncomment the line in your php.ini file for the GD stuff.

But rather than going through all that hassle why not just use html height and width parameters for images

Code: Select all

<? PHP 
<img src="myfile.jpg" width="10" height="10">
?>
Then you only need 1 image

Mike
Matt Wade
Forum Newbie
Posts: 12
Joined: Sat May 04, 2002 7:47 pm

Post by Matt Wade »

resizing with HTML does nothing to the file size. One of the main reasons for thumbnails is to help people out on slow connections pick and choose what images they want to see....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

jepp, sounds like the GD-extension isn't loaded.
If, the following should do the trick

Code: Select all

<?php
if (!array_key_exists('pic',$HTTP_POST_FILES))
	print('<html><body><form enctype="multipart/form-data" method="post"><input type="file" name="pic" /><input type="submit"/></form></body></html>');
else
&#123;
	if(strcasecmp($HTTP_POST_FILES&#1111;'pic']&#1111;'type'], 'image/gif') == 0) // gif-support removed in gd2!
		$resource = @ImageCreateFromGIF ($HTTP_POST_FILES&#1111;'pic']&#1111;'tmp_name']);
	else if (strcasecmp($HTTP_POST_FILES&#1111;'pic']&#1111;'type'], 'image/pjpeg') == 0)
	&#123;
		$resource = @ImageCreateFromJPEG($HTTP_POST_FILES&#1111;'pic']&#1111;'tmp_name']);
	&#125;
	else
		$resource = FALSE;
		
	if (!$resource)
		print('<html><body>error</body></html>');
	else
	&#123;
		if (imagesx($resource)>imagesy($resource))
			$ratio = 60 / imagesx($resource);
		else
			$ratio = 60 / imagesy($resource);
		$thumb = imagecreate(imagesx($resource)*$ratio, imagesy($resource)*$ratio);
		imagecopyresampled( $thumb, $resource, 0, 0, 0, 0, imagesx($resource)*$ratio, imagesy($resource)*$ratio, imagesx($resource), imagesy($resource));
		imagepng( $thumb, $HTTP_POST_FILES&#1111;'pic']&#1111;'name'].'thumb.png');
		print('<html><body><img src="'.$HTTP_POST_FILES&#1111;'pic']&#1111;'name'].'thumb.png"/></body></html>');
	&#125;
&#125;
?>
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Matt Wade wrote:resizing with HTML does nothing to the file size. One of the main reasons for thumbnails is to help people out on slow connections pick and choose what images they want to see....
Sorry Matt, I never saw the bit in his post about wishing to optimise file sizes for users on slow connections. :wink:
User avatar
deeben
Forum Newbie
Posts: 2
Joined: Thu May 16, 2002 10:20 pm
Contact:

would you consider creating the thumbnail at runtime?

Post by deeben »

I found this link by following another one of the posts on this board

http://evolt.org/article/Automated_Crea ... index.html

It gives you a script to make a thumbnail by passing an image name and sizes in the url. I was pleased with the simplicity of the script as I am new to PHP (I will probably be booed for admitting to using ASP for the past few years). I am having difficulty passing parameters to the file when I use it as an included file.

The URL would look like this if I were to load the thumbnailer in the browser

http://www.aussieauto.net/thumbnailer.p ... pg)+x(300)

The image test.jpg is usually 130k but after using the thumbnailer is 20k at a width of 300 pixels. The problem I am having is identifying why it needs the value enclosed in brackets. The script is well commented so I can see that it is using arrays for capturing the variables but as I am new to PHP I don't know where to bypass this and enter the limited number of variables manually.

The code can be viewed at

http://www.aussieauto.net/thumbnailer.php.txt

It is changed slightly from the original to encorpate a change another user made to allow it to use the real filename instead of a unique id from a database. I hope the script helps you out with your thumbnailing and I hope someone can help me out with specific problems with this code.
Madsheep
Forum Newbie
Posts: 2
Joined: Mon May 13, 2002 1:04 am
Location: Wuerzburg, Germany

Thanks for your help.

Post by Madsheep »

Many thank to all of you who sent your suggestions. You helped me a lot! :D
User avatar
deeben
Forum Newbie
Posts: 2
Joined: Thu May 16, 2002 10:20 pm
Contact:

Problem Solved

Post by deeben »

By following the forums on this and a few other sites I have sorted out my problems with passing variables to the array when including the file in another page.

I have recently switched from ASP to PHP and I must say that you should all be proud of the community spirit that surrounds this platform. :wink:
Post Reply