Jump to content

thumbnail part of larger image


jen91

Recommended Posts

hey guys,

 

i am new here and new to php :)  i am trying to create a thumbnail image from part of a larger image when it is uploaded.  i found some code that takes the large image and creates a thumbnail.  this is a proportional thumbnail though.  what i want to be able to do is have it create a thumbnail that is 288 x 125 pixels by just taking the top corner of the large image.  i don't want to shrink it as it gets all distorted.

 

i don't even know if this is possible or if what i typed makes any sense to anyone :D  i couldn't find anything in my searches about this

 

anyways here is the code i am using at the moment

 

	$triplast1 = mysql_insert_id();

$path = '../images/photos/'.$triplast1.'/';
mkdir($path,0777);
chmod($path,0777);

$img = $_FILES['uploadfile'];
$type = substr($img['name'], strrpos($img['name'], '.') + 1);

if(($type == "gif" || $type == "jpg") && $img['size'] < 1024000) {
	$time = time();
	$name = substr($img['name'], 0, strrpos($img['name'], '.'));		
	$fullsizeName	= $name.'_'.$time.'.'.$type;
	$thumbName		= $name.'_'.$time.'_thumb.'.$type;

	if($type == "gif")
		$imgObj = imagecreatefromgif($img['tmp_name']);
	else
		$imgObj = imagecreatefromjpeg($img['tmp_name']);

	$width = imageSX($imgObj);
	$height = imageSY($imgObj);

	if($width > 720) {
	 	$height = $height * (720 / $width);
	 	$width = 720;
	}

	if($height * 1.5 < $width) {
		$thumbWidth = 288;
		$thumbHeight = $height * (288 / $width);
	}
	else {
		$thumbHeight = 125;
		$thumbWidth = $width * (125 / $height);
	}

	$newImage = imagecreatetruecolor($width, $height);
	$newThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
	imagecopyresampled($newImage, $imgObj, 0, 0, 0, 0, $width, $height, imageSX($imgObj), imageSY($imgObj));
	imagecopyresampled($newThumb, $imgObj, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imageSX($imgObj), imageSY($imgObj));

	if($type == "gif") {
		imagegif($newImage, '../images/photos/'.$triplast1.'/'.$fullsizeName);
		imagegif($newThumb, '../images/photos/'.$triplast1.'/'.$thumbName);
	}                     
	else {
		imagejpeg($newImage, '../images/photos/'.$triplast1.'/'.$fullsizeName);
		imagejpeg($newThumb, '../images/photos/'.$triplast1.'/'.$thumbName);
	}                      
	imagedestroy($imgObj);
	imagedestroy($newImage);
	imagedestroy($newThumb);
}
$query = "UPDATE tbltrips SET tmaptmb = '$thumbName', tmaplrg = '$fullsizeName' WHERE tID = '$triplast1'";
$result = mysql_query($query);

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/
Share on other sites

ahhhh i need help :D

 

i have been trying to get this cropping thing working with the existing code but i am struggling

 

i am using this code

 

$path = '../images/photos/'.$triplast1.'/';
mkdir($path,0777);
chmod($path,0777);

$img = $_FILES['uploadfile'];
$type = substr($img['name'], strrpos($img['name'], '.') + 1);

if(($type == "gif" || $type == "jpg") && $img['size'] < 1024000) {
	$time = time();
	$name = substr($img['name'], 0, strrpos($img['name'], '.'));		
	$fullsizeName	= $name.'_'.$time.'.'.$type;
	$thumbName		= $name.'_'.$time.'_thumb.'.$type;

	if($type == "gif")
		$imgObj = imagecreatefromgif($img['tmp_name']);
	else
		$imgObj = imagecreatefromjpeg($img['tmp_name']);

	$width = imageSX($imgObj);
	$height = imageSY($imgObj);

	if($width > 720) {
	 	$height = $height * (720 / $width);
	 	$width = 720;
	}

	$thumbw = 288;
	$thumbh = 125;

	$newImage = imagecreatetruecolor($width, $height);
	$newThumb = imagecreatetruecolor($thumbw, $thumbh);

	$wm = $width/$thumbw;
	$hm = $height/$thumbh;
	$h_height = $thumbh/2;
	$w_height = $thumbw/2;

	imagecopyresampled($newImage, $imgObj, 0, 0, 0, 0, $width, $height, imageSX($imgObj), imageSY($imgObj));

	$adjusted_width = $width / $hm;
	$half_width = $adjusted_width / 2;
	$int_width = $half_width - $w_height;

	imagecopyresampled($newThumb, $imgObj, -$int_height, 0, 0, 0, $adjusted_width, $thumbh, $width, $height);

	if($type == "gif") {
		imagegif($newImage, '../images/photos/'.$triplast1.'/'.$fullsizeName);
		imagegif($newThumb, '../images/photos/'.$triplast1.'/'.$thumbName,100);
	}                     
	else {
		imagejpeg($newImage, '../images/photos/'.$triplast1.'/'.$fullsizeName);
		imagejpeg($newThumb, '../images/photos/'.$triplast1.'/'.$thumbName,100);
	}                      
	imagedestroy($imgObj);
	imagedestroy($newImage);
	imagedestroy($newThumb);
}
$query = "UPDATE tbltrips SET tmaptmb = '$thumbName', tmaplrg = '$fullsizeName' WHERE tID = '$triplast1'";
$result = mysql_query($query);

 

it is creating the folder, larger image and thumbnail which is fine.  the problem is that the thumnail is not cropping, is is just scaling so i end up with a small image and the rest of it is just black background.

 

i have tried a few different things but i cant get it to work

 

please please please can somebody help me :D

$width = imageSX($imgObj);
$height = imageSY($imgObj);
.....
imagecopyresampled($newImage, $imgObj, 0, 0, 0, 0, $width, $height, imageSX($imgObj), imageSY($imgObj));

 

i think $width is same as imageSX($imgObj) and $height is same as imageSY($imgObj). so ur passing the destination height and width the same as source, so it will give u image of the same size, it crops the image but the size is same so it fills remaining area with black color..

 

please carefully study each parameter of the function imagecopyresampled and check if you are supplying them correctly.

 

 

 

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.