Forum Moderators: coopster

Message Too Old, No Replies

How to create 2 images from same image file?

$_FILES,php, images

         

rodriguez1804

4:27 am on Apr 21, 2010 (gmt 0)

10+ Year Member



Hey all!

I am trying to create 2 re-sized images from 1 source image that the user uploads through a form. I have the following code:

The following is a piece of code for the class to re-size the image files

class resizer{
...
function resize($filename,$width,$height){
$img=$filename;
...
$thumb = 'the resized pic';
ImageJPEG($thumb,$img);
}}


The class above works fine and gives the picture as expected, but when I try to use the class in another file, like shown below, it only gives me 1 picture instead of 2. It's overriding the other picture and I don't know why?


require_once("resizer.php");
$resizeImage = new resizer();
$resizeImage->resize($_FILES['mypicture']['tmp_name'],250,250);

$resizeImage2 = new resizer();
$resizeImage2->resize($_FILES['mypicture']['tmp_name'],100,'100');


From the above code, I am only able to get THE LAST resized image of 100x100, but I want both images? What am I doing or not doing right, and how do I fix this? Thanks a bunch!

Matthew1980

7:08 am on Apr 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there rodriguez1804,

I could be wrong, as I don't use this function at all, but from how I read this, maybe you should just include("resizer.php"); instead of require_once("resizer.php"); as this probably does as advertised. I may be completely wrong though!

Whenever referenceing files I find that the best approach is to use include("absolute/path/to/your/file/name.php"); by doing the entire filepath you can eliminate the classic parse error "failed to open stream", sometimes even good practise to use file_exists(); too, but that's just a preference thing.

Your reference to the class seems fine as you are creating to two explicit $vars as you would in a standard function.

The only other thing as could be a cause is that you are overwriting the first instance with the second, you may need to create a distinct second file, unless the picture filenames are different, that said, are you renaming the file from the original submitted one somewhere in the class?

Cheers,
MRb

rodriguez1804

1:04 pm on Apr 21, 2010 (gmt 0)

10+ Year Member



Hi Matthew1980,

Thanks for the reply. I tried creating a separate file and using include instead, but it still seems to override the first image. I am not renaming the file anywhere inside the resizer class.

I am thinking it may have something to do with the way I try to copy the images to their respective folders. This is what I am using:

$copied = copy($_FILES['mypicture']['tmp_name'], $newname);
$copiedBig = copy($_FILES['mypicture']['tmp_name'],$newnameBig);

In both cases, I am using the the same $_FILES['mypicture']['tmp_name']. I don't know how else to call it though.

rainborick

5:22 pm on Apr 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It seems that you're calling the same function on the uploaded file twice, so you just get the result of the last call. You need to copy $_FILES['mypicture']['tmp_name'] to your image objects, then manipulate (ie. resize) those image objects directly.

rocknbil

5:38 pm on Apr 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(rainborick beat me to it. :-) )

It is probably doing it twice. You're overwriting the first, and this class appears to have no support for both the full image and the thumb. Try something like this . . . make backups first. :-)

Also this has **zero** support for anything but jpg, note how I've worked that in. You'll have to debug it, this is all on the fly, may contain errors but will get you where you need to go. Note important exceptions at bottom.


require_once("resizer.php");
//
$ex=$thumbname=$thumbpath=$large_path=NULL; // Initialize
$big_width = 250; // See notes . . . this has problems
$big_height = 250;
$t_width = 100;
$t_height = 100;
$name = $_FILES['mypicture']['name'];
// To create a thumb, we need to split on the extension. Watch...
list ($nm, $ex) = preg_split("/\./",$name,-1,PREG_SPLIT_NO_EMPTY);
$type = $_FILES['photo']['type'];
$size = $_FILES['photo']['size'];
$tmp_name = $_FILES['photo']['tmp_name'];
$error = $_FILES['photo']['error'];
$uploads_dir = $_SERVER['DOCUMENT_ROOT'] . '/your-upload-directory';
//
// error trapping is your friend. Here I'm just echoing and exiting,
// not best but with the info provided, best I can show you.
if (! ($size > 0)) { echo "<p>The data upload failed with a zero byte image file.</p>\n"; exit; }
if ($error > 0) { echo "<p>An unknown error occurred on upload.</p>\n"; exit; }
if (! preg_match('/jpe*g|gif|bmp|png/i',$type)) {
echo '<p>You can only upload images in jpg, gif, or png format.</p>'; exit;
}
//
// If no extension found (err, Macintosh) assume jpg and pray.
if (! isset($ex)){
$ex = (preg_match("/(jpe*g|gif|png|bmp)/i",$type))?".$1":'.jpg';
$name .= $ex;
}
$thumbname = $nm . '_tn' . $ex;
// You need to think in terms of full server path for uploads.
$thumbpath = "$uploads_dir/" . $thumbname;
$large_path = "$uploads_dir/" . $name;
//
// Okay . . . NOW . . . note we're passing the full path to
// where we want the files, and the TYPE so we can support other types.
// If we move it first, no need to reference $tmp_name in the function.
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$resizeImage = new resizer();
$resizeImage->resize($type,$large_path,$thumbpath,
$big_width,$big_height,$t_width,$t_height);


Now for the class changes, accept the extra parameters.


class resizer{
//
function resize($img_type,$img,$thumbname,$width,$height,$twidth,$theight){
//
// GD Function List, with our *allowed* upload types
$gfunctions = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG'
);
//
$img_info = GetImageSize($img);
if (! (($img_info[0]>0) and ($img_info[1]>0))) { echo "<p>Error occured getting image size.</p>"; exit; }
$w = $img_info[0];
$h = $img_info[1];
$func = 'imagecreatefrom' . $gfunctions[$type];
$write = 'image' . $gfunctions[$type];
// Note "$func" = imagecreatefromjpg, imagecreatefromgif, etc.
$src = $tn = $func($img); // so we resize from the original both times
// big image
$tmp = imagecreatetruecolor($width,$height);
imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$w,$h);
// Note "$write" = imagejpg, imagegif, etc.
$write($tmp,$img,100); // last param = quality
// thumb
$tmp=imagecreatetruecolor($twidth,$theight);
imagecopyresampled($tmp,$tn,0,0,0,0,$twidth,$theight,$w,$h);
$write($tmp,$tn,75);
}
}


Exceptions and problems with the above:

If this were me, I'd move as much as I could to the function and pass a minimal set of parameters. All the sizing, move_uploaded_file, determinig type, etc., al should go in the function. The example above is taken from "where you are now."

When you resize the images, you will get distortion if they are not square. So what you should do is set a configuration for, say, "max_image_width" and "max_thumb_width" and pass those to your function, like

$resizeImage->resize($tmp_name,$max_image_width,$max_thumb_width,$large_path,$thumbpath);

Then, within the function, extract the image sizes from a simple if case . . .

if ($w > $img_width) {
$width=$img_width;
$height=intval(($img_width * $h) / $w);
}

... duplicate for thumb width. Depending on the scenario, you can create the extension directly in the function using


// Allowed file types, note NOT the same as the functions
$ptypes = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
);
$ext = $known_photo_types[$type];
$name = $name . '.' . $ext;
$thumbname = $name . '_tn.' . $ext;


Which makes it even easier, you just need to pass the upload directory, extracting the $name from the uploaded file name. That too has problems, it will overwrite existing files, so more file system checks and responses . . .

$resizeImage->resize($tmp_name,$max_image_width,$max_thumb_width,$upload_dir);

Lots of ways to go with this one. :-)

rodriguez1804

6:53 am on Apr 22, 2010 (gmt 0)

10+ Year Member



got it working. Thanks everyone!