Jump to content

Thumbnail function working funny


waynew

Recommended Posts

For some reason, if the width of the image is largely bigger than the height, the thumbnail ends up being a 70 x 70 black image.

 

function cropImage($nw, $nh, $source, $stype, $dest) {
	$size = getimagesize($source);
	$w = $size[0];
	$h = $size[1];

	switch($stype) {
		case '.gif':
		$simg = imagecreatefromgif($source);
		break;
		case '.jpg':
		$simg = imagecreatefromjpeg($source);
		break;
		case '.jpeg':
		$simg = imagecreatefromjpeg($source);
		break;
	}
	$dimg = imagecreatetruecolor($nw, $nh);
	$wm = $w/$nw;
	$hm = $h/$nh;
	$h_height = $nh/2;
	$w_height = $nw/2;
	if($w> $h) {
		$adjusted_width = $w / $hm;
		$half_width = $adjusted_width / 2;
		$int_width = $half_width - $w_height;
		imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
	} 
	elseif(($w <$h) || ($w == $h)) {
		$adjusted_height = $h / $wm;
		$half_height = $adjusted_height / 2;
		$int_height = $half_height - $h_height;
		imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
	} 
	else {
		imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
	}
	imagejpeg($dimg,$dest,100);
}

Link to comment
https://forums.phpfreaks.com/topic/125056-thumbnail-function-working-funny/
Share on other sites

-1*a = -a

In fact, -1*a would be simplified to -a by 99% of the world x.x.

 

It's entirely valid in PHP to do -$var.

 

 

Hrmmm, the third param for imagecopyresampled() is dst_x.  Why would that be negative?  That doesn't make sense to me.  If I have a 70x70 grid, with 0,0 being in the top left corner, and 70,70 being in the bottom right (like the top right quadrant of a Cartesian coordinate plane [iE normal graph]), for x,y if x is negative, then as long as x is negative, it won't be shown.  So, if you have -a as your new x, the area shown would be 70-a (ignoring y's).

 

 

So, to put things simply, the negative is probably what is screwing things up.  Are you sure you really want the number to be negative?

 

 

 

for bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

 

shouldn't you be doing something like:

 

imagecopyresampled(dst_image, src_image, 0, 0, 0, 0, 70, 70, src_w, src_h);

 

?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.