• 0

[php] creating thumbnails (GD Library)


Question

Hi, i have created a script that uploads photos and adds them to the database, which are then displayed on a page, however they are coming out too large and taking forever to load, so i am trying to create thumbnails using GD Library.

Now i've already came up with a bulk of code with help from people and tutorials etc.. but i don't know how to add thumb_ onto the start of the file.

I want it to resize image.jpg and then name it thumb_image.jpg

The script is also in a different folder to the images.

I'm not that experienced with PHP, still learning, can anyone point me in the right direction?

Also there are 6 photos being uploaded at a time, so i need it to resize all 6 images, i've pushed all the image into their own variable.

$photo1 = $_FILES['picture']['name'][0];

$photo2 = $_FILES['picture']['name'][1];

$photo3 = $_FILES['picture']['name'][2];

$photo4 = $_FILES['picture']['name'][3];

$photo5 = $_FILES['picture']['name'][4];

$photo6 = $_FILES['picture']['name'][5];

Here is the code so far:


$max_width = 150;
$max_height = 150;

list($orig_width, $orig_height, $type) = getimagesize($image_file);
$xratio = $orig_width / $max_width;
$yratio = $orig_height / $max_height;

if (($orig_width > $max_width) || ($orig_height > $max_height))
{
if ($xratio > $yratio)
{
$dest_width = $max_width;
$dest_height = $orig_height / $xratio;
}
else
{
$dest_height = $max_height;
$dest_width = $orig_width / $yratio;
}

if ($type == IMAGETYPE_JPEG) $orig_image = imagecreatefromjpeg($image_file);
elseif ($type == IMAGETYPE_BMP) $orig_image = $this->ImageCreateFromBMP($image_file);
elseif ($type == IMAGETYPE_GIF) $orig_image = imagecreatefromgif($image_file);
elseif ($type == IMAGETYPE_PNG) $orig_image = imagecreatefrompng($image_file);
else return; // Do nothing!

$new_image = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($new_image, $orig_image, 0, 0, 0, 0, $dest_width, $dest_height, $orig_width, $orig_height);

if ($type == IMAGETYPE_JPEG) imagejpeg($new_image, $image_file, 85);
elseif ($type == IMAGETYPE_BMP) imagejpeg($new_image, $image_file, 85);
elseif ($type == IMAGETYPE_GIF) imagegif($new_image, $image_file);
elseif ($type == IMAGETYPE_PNG) imagepng($new_image, $image_file);

}[/PHP]

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0


if ($type == IMAGETYPE_JPEG) imagejpeg($new_image, "thumb_" . $image_file, 85);
elseif ($type == IMAGETYPE_BMP) imagejpeg($new_image, "thumb_" . $image_file, 85);
elseif ($type == IMAGETYPE_GIF) imagegif($new_image, "thumb_" . $image_file);
elseif ($type == IMAGETYPE_PNG) imagepng($new_image, "thumb_" . $image_file);

}[/PHP]

Assuming $image_file contains just the name of the image, and not the full path to the image, then you can use the above. You would just prepend the "thumb_" text onto the $image_file variable.

However, as written, it will write the file to the current working directory, so you may want to also add a path to that as well to ensure they are saved where you want them.

Link to comment
Share on other sites

  • 0

I am a little bit further forward now i managed to simplify it.

First of all i put the create thumbnail script into a function like so:

<?php
function createThumbnail($name) {

	$uploads_dir = "../cars/used/";
	$thumbs_dir = "../cars/used/thumb/";
	$max_width = 200;

	$im = imagecreatefromjpeg($uploads_dir . $name);

	$ox = imagesx($im);
	$oy = imagesy($im);

	$nx = $max_width;
	$ny = floor($oy * ($max_width / $ox));

	$nm = imagecreatetruecolor($nx, $ny);

	imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

	imagejpeg($nm, $thumbs_dir . $name);
}
?>

and i have this upload script which also works:

if (!empty($_FILES)) {
	foreach($_FILES['picture']["error"] as $key =>$error) {
		if ($error == UPLOAD_ERR_OK) {
			$tmp_name = $_FILES["picture"] ["tmp_name"][$key];
			$name = $_FILES["picture"]["name"][$key];
			if (move_uploaded_file($tmp_name, "$uploads_dir/$name")) {
				$uploaded = TRUE;
				$uploaded_files++;
			} else {
				$skipped_files++;
			}
		} else {
			$skipped_files++;
		}

	}
}

I don't know where to insert the createThumbnail function into the upload script to make it run through and generate a thumbnail for THAT photo in the cycle, remember it needs to go through all 6 photos.

Both scripts work on their own and i've tried inserting the function in different places but then it doesnt upload the image or generate thumbnails, also it doesn't give me any errors the script does complete, i know this because the last thing it does if the upload is sucessful is add the info to the database.

Am i able to insert the function anywhere into that script?

Link to comment
Share on other sites

  • 0
...
                        if (move_uploaded_file($tmp_name, "$uploads_dir/$name")) {
                                $uploaded = TRUE;
                                $uploaded_files++;
				createThumbnail("$uploads_dir/$name")
                        } else {
                                $skipped_files++;
                        }
...

Link to comment
Share on other sites

  • 0

Thanks for that mate, it's shooting ou this error, relating to the path:

Warning: imagecreatefromjpeg(../cars/used../cars/used//DSC03037.JPG) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/uplift/public_html/admin/functions.php on line 8

I know it's going to be something simple but i've been fiddling with the paths n stuff for half hour and haven't got it working lol. Any ideas?

Line 8: $im = imagecreatefromjpeg($uploads_dir . $name)

I just clicked on and removed $uploads_dir from the line

now it shoots out:

Warning: imagejpeg() [function.imagejpeg]: Unable to open '../cars/used/thumb../cars/used//DSC03037.JPG' for writing: No such file or directory in /home/uplift/public_html/admin/functions.php

on line 20

Link to comment
Share on other sites

  • 0

I fixed the error. I needed the variables inside the function, but this was causing a conflict with the uploads_dir outside of the function, so i renamed it to uploads_dir1 and now it works.

Link to comment
Share on other sites

  • 0

Or you could always resize to create a "thumbnail" versus actually creating an thumbnail image.

http://code.google.com/p/timthumb/ - A small php script for cropping, zooming and resizing web images (jpg, png, gif). Perfect for use on blogs and other applications.

that was the problem, the page could have up to 100 images on so just setting a width of lots of large images was taking forever to load

Link to comment
Share on other sites

  • 0

Well having 100 images on a single page would increase the load regardless.. Add pagination and that would greatly decrease load time and strain on the server.

Link to comment
Share on other sites

  • 0

Well having 100 images on a single page would increase the load regardless.. Add pagination and that would greatly decrease load time and strain on the server.

Yes but not to a significant lag. I have pagination, 20 cars per page and 6 thumbnails per car. 120 images.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.