[SOLVED] Why won't "imagecopyresampled" resize the

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
RightOnLM
Forum Newbie
Posts: 4
Joined: Tue Jul 20, 2004 1:11 am
Location: California

[SOLVED] Why won't "imagecopyresampled" resize the

Post by RightOnLM »

Can someone please help me? I am totally frustrated trying to create a thumbnail image from a image uploaded through a form. I've never posted anything before so I hope my explaination isn't too long. Here it goes...

I have some code that will upload a jpeg image through a form, scale it to a specific size, save it to my server and enter the file name into a mySQL table. This all works perfectly. The problem comes when I try to also create a thumbnail from the image that I just uploaded and save it into a /thumbs directory. Here is the function that does the resize:

Code: Select all

<?php

$imgsize = getimagesize($userfile1);
if(($imgsize[0] > 350 || $imgsize[1] > 350))
{
    $tmpimg = tempnam("/tmp","MKUP");
					
    // resize large image
    system("djpeg $userfile1 > $tmpimg");
    system("pnmscale -xy 350 350 $tmpimg | cjpeg -smoo 10 -qual 90 >$userfile1");
				
    unlink($tmpimg);
}		

// process the image file uploaded by the form
$safefilename = ereg_replace("[^A-Za-z0-9._]", "",str_replace(" ", "_",strtolower($_FILES['userfile1']['name'])));
$destfilepath = $imagepath.$safefilename;
$destfilepath_thumb =  $imagepath.'thumbs/t_'.$safefilename;

// move the image file to the directory on the server
if (!move_uploaded_file($_FILES['userfile1']['tmp_name'], $destfilepath))
{
    echo "<p><strong>There was a problem uploading your image</strong>.</p>";
    exit;
}

// $destfilepath  =  "/home/rightons/public_html/client/staging/levonian/gallery/uploaded_images/body_center_image.jpg" ** these are the values that are being passed into the function, shown here for information only

// $destfilepath_thumb = "/home/rightons/public_html/client/staging/levonian/gallery/uploaded_images/thumbs/t_body_center_image.jpg"


createthumb($destfilepath,$destfilepath_thumb,60,60);


function createthumb($name,$filename,$new_w,$new_h){
	echo $name;
	$system=explode(".",$name);
	if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
	if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	if ($old_x > $old_y) {
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y) {
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y) {
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$gd2 = "yes"; // force imagecopyresampled
	if ($gd2==""){
			$dst_img=ImageCreate($thumb_w,$thumb_h);
			imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
	}else{
		$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);  
	}
	if (preg_match("/png/",$system[1])){
		imagepng($dst_img,$filename); 
	} else {
		imagejpeg($dst_img,$filename); 
	}
	imagedestroy($dst_img); 
	imagedestroy($src_img); 
}

?>
The file is opened and saved into the thumbs directory but it isn't resized. It keeps the same dimension as the original image. Why? I get no error messages. The correct image sizes are being passed into the imagecopyresampled function.

Please, any help would be very appreciated. It shouldn't be that difficult to make a thumbnail! Aargh!!
lolpix
Forum Commoner
Posts: 41
Joined: Sat Jul 17, 2004 2:20 am

Post by lolpix »

Am I mistaken, or have you invoked the createthumb() function before defining it?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sorry, currently unable to test it locally. But just wanted to reply to lolpix; Doesn't matter here.

Code: Select all

echo something('foo');
    function something($string) {
        return 'bar';
    }
  // prints bar
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Moving to Graphics
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The problem could possibly by from stripping all periods out of the filename, then exploding on periods..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Is $src_img being created for certain?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RightOnLM
Forum Newbie
Posts: 4
Joined: Tue Jul 20, 2004 1:11 am
Location: California

Post by RightOnLM »

when I echo the $src_img variable I get "Resource id #12" Could this be the problem? Remember, the thumbnail image gets created but it gets created without being resized, it keeps the original dimensions.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

No, that's not the problem. It outputs that because $src_img is not a variable that can be output in a human readable form.

Echo $thumb_w and $thumb_h right before you call ImageCreate - your math might be wrong. Speaking of which, what's the purpose of:

Code: Select all

if ($old_x > $old_y) {
      $thumb_w=$new_w;
      $thumb_h=$old_y*($new_h/$old_x);
   }
   if ($old_x < $old_y) {
      $thumb_w=$old_x*($new_w/$old_y);
      $thumb_h=$new_h;
   }
   if ($old_x == $old_y) {
      $thumb_w=$new_w;
      $thumb_h=$new_h;
   }
If you're just trying to maintain ratios, I'm sure it can be done simpler :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RightOnLM
Forum Newbie
Posts: 4
Joined: Tue Jul 20, 2004 1:11 am
Location: California

Post by RightOnLM »

here is my output of $thumb_w and $thumb_h just before I call

Code: Select all

<?php

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

?>
40.4571428571, 60

any clues?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Very few to be honest, try

Code: Select all

echo imagesx($dest_img);
echo imagesy($dest_img);
Seems kind of pointless, but you never know :?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RightOnLM
Forum Newbie
Posts: 4
Joined: Tue Jul 20, 2004 1:11 am
Location: California

Why won't "imagecopyresampled" resize the image? H

Post by RightOnLM »

It's a miracle!!!

I just tested it again and it works! I don't know what happened, I don't think that I modified anything, but it is working! Pickle, thank you so much for helping me with this.

Consider this resolved!

Thanks!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Not sure what I did exactly, but you're welcome :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply