Log in

View Full Version : Saving a resized image...



Moshambi
12-21-2009, 06:45 AM
Hi all,

I have some code that resizes an image. It works fine but what I want to do next is to take the scaled image and save it as a thumbnail, that way I don't have to resize a hundred images everytime the page loads but instead just call it from it's saved filename. I have looked at many different things and I cannot seem to find a solution. Please help!

Thanks!

djr33
12-21-2009, 08:25 AM
You should post the code you are using. There are different ways to manipulate images in PHP. The most common is with the GD library, an included set of functions for images. Others, like imagemagick, also exist.

Assuming you are using GD, then it's simple:
the functions imagegif(), imagepng(), and imagejpeg() will output a file directly:
imagegif($image); //outputs an image
But if you use a second parameter of a file path, it will save it as that image:
imagegif($image,$path); //save $image to $path

Moshambi
12-28-2009, 09:02 PM
Thanks for your response. Sorry I had not been on in a while to see what you posted. I think I am using the GD library, I'll have to get the code for you to look at when I get home. But I think the problem is that it takes an image scales it down and then all it does is return a created <img> tag, which I don't like so I am going to need to find a better way I'm guessing.

bluewalrus
12-28-2009, 10:16 PM
Assuming your using http://pear.php.net/package/Image_Transform you can use this code to take an uploaded image and out put 2 scaled down images.



$upload_file = basename($_FILES['uploadfile']['name']);
// create a file with just a 0 in it to keep track of uploads or don't and remove all instances of these 2 variables
$log_file = "num.txt";
$file_num = file_get_contents($log_file);
//those 2
$dir = "images/";
$thumb = "thumb/";
$output_format = ".jpg";
// set the directory for where the file file is to go
$uploaddir = "images/original/";
$file_name = basename($_FILES['uploadfile']['name']);
//set the path and file name for the move file
$file = $uploaddir . $file_name;
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
echo "Success";
require_once 'Image/Transform.php';
//
//create transform driver object
$it = Image_Transform::factory('GD');
if (PEAR::isError($it)) {
die($it->getMessage());
}
//load the original file
$ret = $it->load("$file");
if (PEAR::isError($ret)) {
die($ret->getMessage());
}
//scale it to 100 pixels wide
$ret = $it->scaleByLength(100);
if (PEAR::isError($ret)) {
die($ret->getMessage());
}
//save it into a different file
$ext = substr($file_name,strlen($file_name)-3,3);
$clear_ext = explode("." . $ext, $file_name);
$file_name = $clear_ext[0];
$ret = $it->save("$dir$custom$thumb$file_name" . "_file" . $file_num . $output_format);
if (PEAR::isError($ret)) {
die($ret->getMessage());
}
//load the original file
$ret = $it->load("$file");
if (PEAR::isError($ret)) {
die($ret->getMessage());
}
//scale it to 500px
$ret = $it->scaleByLength(500);
if (PEAR::isError($ret)) {
die($ret->getMessage());
}
//save it into a different file
$ret = $it->save("$dir$custom$file_name" . "_file" . $file_num . $output_format);
if (PEAR::isError($ret)) {
die($ret->getMessage());
}
$file_num++;
file_put_contents($log_file, $file_num);
echo $file_num;
} else
{
echo "error ".$_FILES['uploadfile']['error']." --- ".$_FILES['uploadfile']['tmp_name']." %%% ".$file."($size)";
}


This is shortened down from a larger file. If this doesn't work and you have library installed post back and I'll check what part I cut out that you needed hah.

Moshambi
12-29-2009, 07:10 PM
Hmm...I don't think I am using pear. The snippet I got was from php.net in someones post.