hi
how can we minus 15 pixel from bottom height in fitting for create thumbnails from original image in this code ? i mean how can tell php that capture from original image the ( all width ) and ( all height - 15 pixel from bottom )

# #create thumb
# $thumb = gd_fit($image,$settings['thumbxy']);



#  function gd_fit(&$image, $target) { 
#  
#         //takes the larger size of the width and height and applies the  
#         //formula accordingly...this is so this script will work  
#         //dynamically with any size image 
#  
#         $width = gd_width($image);
#         $height = gd_height($image);
#  
#         if ($width > $height) { 
#             $percentage = ($target / $width); 
#         } else { 
#             $percentage = ($target / $height); 
#         } 
#  
#         //gets the new value and applies the percentage, then rounds the value 
#         $width = round($width * $percentage); 
#         $height = round($height * $percentage); 
#  
#         //returns the new sizes in html image tag format...this is so you 
#         //can plug this function inside an image tag  
#  
#         return gd_resize($image,$width,$height);
#  
#     }

Recommended Answers

All 17 Replies

Member Avatar for diafol

Try this - I messed around with it for a while, but I'm sure it could be further optimized - perhaps with just the one imagecopyresampled - but my head's fried.

<?php
function CutThumb($img){
//set variables
	$crop = 15;
	$thumb_w = 100;
	$thumb_h = 100;
//get info from original image
 	list($w, $h) = getimagesize($img);
//make image a resource for use in functions	   
    $img_r = imagecreatefromjpeg($img);
//make image containers for functions	
	$cut = imagecreatetruecolor($w,$h-$crop);
	$thumb = imagecreatetruecolor($thumb_w,$thumb_h);
//crop the original image 
	imagecopyresampled($cut,$img_r,0,0,0,0,$w,$h,$w,$h-$crop);
//resize the image	
	imagecopyresampled($thumb,$cut,0,0,0,0,$thumb_w,$thumb_h,$w,$h-$crop);
//release memory
    imagedestroy($img_r);
	imagedestroy($cut);
//return image    
	return $thumb;
}

//Create the thumbnail - this could be in any file if the function above is included
$MyCutThumb = CutThumb("burton.jpg");
// Display image
header('Content-type: image/jpeg');
imagejpeg($MyCutThumb);
?>

BTW - I left out any "constrain proportions" code.

hi dear ardav
Thanks for reply
but it would crop the original image , i just need it without affecting in the original image ?

i have the code above that my script working with it's using function gd_fit , it's fitting that image with variables , so can we try to edit this code to add $crop = 15 and minus it from height ? i don't know how to do it exactly

Member Avatar for diafol

Have to be honest - I don't really understand exactly what you want:

Do you just want to resize the image (height only)?
e.g. just take 15px off the height and don't worry about stretching the width

Do you just want to resize the height and keep the width in proportion?
e.g. take 15 px off the height of the image and scale the width accordingly


Do you need to crop the image? I assume not from your last post.

Thanks Dear ardav for reply

i just want to crop 15 pixel from the thumbnails only not the original image , why ? because all images i upload have my own watermark on the bottom this watermark size 15 pixel in the bottom , and i just want that watermark disappear in ( thumbnails only)
can i send you the script to take a look ?

Member Avatar for diafol

Thanks Dear ardav for reply

i just want to crop 15 pixel from the thumbnails only not the original image , why ? because all images i upload have my own watermark on the bottom this watermark size 15 pixel in the bottom , and i just want that watermark disappear in ( thumbnails only)
can i send you the script to take a look ?

By all means, as long as it is posted to the forum. I do not accept PMs. Anyway, I can't promise to help and perhaps another member can give better advice.


What we need to know is the nature of your thumbnail - is it set as a %age of a width or height or is it set to a certain pixel height or width. AND is it "constrained" (scaled to proportion)?

Your solution will be similar to the one I suggested, except that the resizing is completed first and then the cropping (opposite to my suggestion).

Member Avatar for diafol

Sorry F, I'm not going to download them. If you want to post the relevant bits, we'll have a look.

ok dear i will copy codes that i think involved with the issue

here that in uploud.php

#create thumb
					$thumb = gd_fit($image,$settings['thumbxy']);
					
					#Accomodate image options
					if(isset($_REQUEST['border']) || isset($_POST['border'])) 
						gd_rectangle($image,0,0,gd_width($image)-1,gd_height($image)-1,"000000");
					if(isset($_REQUEST['bordert']) || isset($_POST['bordert'])) 
						gd_rectangle($thumb,0,0,gd_width($thumb)-1,gd_height($thumb)-1,"000000");
					

					#save thmb first
					imagejpeg($thumb,"$th/$id.jpg",$settings['thumbuality']);
					imagedestroy($thumb);
					#then save actual jpeg
					imagejpeg($image,"$im/$id.jpg",$settings['quality']);
					imagedestroy($image);
					
					$iptc = new iptc("$im/$id.jpg");
					if($cimg[0] == 1) {
						#it is gif keep in data
						write_file("$im/$id.jpg",load_file("$th/$id.jpg"));
						write_file("$im/$id.gif",$cimg[1]);
						$iptc->set(DPI_GIF,"GIF");
					}

functions that in dpi_ext.php (and think it's involved with the issue

function gd_fit(&$image, $target) { 
    
        //takes the larger size of the width and height and applies the  
        //formula accordingly...this is so this script will work  
        //dynamically with any size image 
        
        $width = gd_width($image);
        $height = gd_height($image);
        
        if ($width > $height) { 
            $percentage = ($target / $width); 
        } else { 
            $percentage = ($target / $height); 
        } 
        
        //gets the new value and applies the percentage, then rounds the value 
        $width = round($width * $percentage); 
        $height = round($height * $percentage); 
        
        //returns the new sizes in html image tag format...this is so you 
        //can plug this function inside an image tag and just get the 
        
        return gd_resize($image,$width,$height);
        
    } 
    function gd_getfit(&$image, $target) { 
    
        //takes the larger size of the width and height and applies the  
        //formula accordingly...this is so this script will work  
        //dynamically with any size image 
        
        $width = gd_width($image);
        $height = gd_height($image);
        
        if ($width > $height) { 
            $percentage = ($target / $width); 
        } else { 
            $percentage = ($target / $height); 
        } 
        
        //gets the new value and applies the percentage, then rounds the value 
        $width = round($width * $percentage); 
        $height = round($height * $percentage); 
        
        //returns the new sizes in html image tag format...this is so you 
        //can plug this function inside an image tag and just get the 
        
        return Array('width'=>$width,'height'=>$height);
        
    }     
    
        function gd_width(&$image) {
        return imagesx($image);
    }
    function gd_height(&$image) {
        return imagesy($image);
    }    

    function gd_imagewidth($fname) {
        list($width, $height) = getimagesize($fname);         
        return $width;
    }
    function gd_imageheight($fname) {
        list($width, $height) = getimagesize($fname);         
        return $height;
    }       
    
        function gd_fit_bg(&$img,$fit=100,$color="FFFFFF") {
        $w = imageSX($img);
        $h = imageSY($img);
        $img2 = gd_newimage($fit,$fit);
        gd_bar($img2,0,0,$fit,$fit,$color);
        
        $x = ($fit-$w)/2;
        $y = ($fit-$h)/2;
        
        imagecopy($img2, $img, $x,$y, 0, 0, $w, $h); 
        return $img2;
    }
        
	    function gd_resize(&$image,$w,$h) {
        $ret=@imagecreatetruecolor($w, $h); 
        @imagecopyresampled ($ret,$image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));  
    
        return $ret; 
    }

other functions in dpi_ext.php

function dpi_supported($file) { 
		$DPI_IMAGE_TYPES = Array(1,2,3,10);
		$size = @getimagesize($file);
		if(!$size) return false;
		return in_array($size[2],$DPI_IMAGE_TYPES) ? $size[2] : false; 
	}
	

	function scanlines_h(&$image,$line_gap=1,$opacity=100) {
		$w = imagesx($image);
		$h = imagesy($image);
		$ret = imagecreatetruecolor($w,$h);
		$line_gap++;
		
		for ($y = 1; $y < $h; $y += $line_gap) 
			imagecopymerge($ret, $image, 0, $y, 0, $y, $w, 1,$opacity);
		
		return $ret;
	}
	function scanlines_v(&$image,$line_gap=1,$opacity=100) {
		$w = imagesx($image);
		$h = imagesy($image);
		$ret = imagecreatetruecolor($w,$h);
		$line_gap++;
		
		for ($x = 1; $x < $w; $x += $line_gap) 
			imagecopymerge($ret, $image, $x, 0, $x, 0, 1, $h,$opacity);
		
		return $ret;
	}	
	function shear_r(&$image,$pers=50,$bgcolor="FFFFFF") {
		$w = imagesx($image);
		$h = imagesy($image);
		$ret = imagecreatetruecolor($w,$h);
		gd_bar($ret,0,0,$w,$h,$bgcolor);
		$z=0;
		$pers = 100-$pers;
		$pers /=100;
		$p1 = $h * $pers;
		
		$gap = $p1 / $w;
		
		for ($x = 0; $x < $w; $x++,$z+=$gap) 
			imagecopyresampled ( 
				$ret, 	#resource dst_im, 
				$image,	#resource src_im, 
				$x, 		#int dstX, 
				($h-($h-$z))/2, 		#int dstY, 
				$x,		#int srcX, 
				0,		#int srcY, 
				1,		#int dstW, 
				$h-$z,	#int dstH, 
				1,		#int srcW, 
				$h		#int srcH
			);
		
		return $ret;
	}		
	function shear_l(&$image,$pers=50,$bgcolor="FFFFFF") {
		$w = imagesx($image);
		$h = imagesy($image);
		$ret = imagecreatetruecolor($w,$h);
		gd_bar($ret,0,0,$w,$h,$bgcolor);
		$z=0;
		$pers = 100-$pers;
		$pers /=100;
		$p1 = $h * $pers;
		
		$gap = $p1 / $w;
		
		for ($x = $w-1; $x >= 0; $x--,$z+=$gap) 
			imagecopyresampled ( 
				$ret, 	#resource dst_im, 
				$image,	#resource src_im, 
				$x, 		#int dstX, 
				($h-($h-$z))/2, 		#int dstY, 
				$x,		#int srcX, 
				0,		#int srcY, 
				1,		#int dstW, 
				$h-$z,	#int dstH, 
				1,		#int srcW, 
				$h		#int srcH
			);
		
		return $ret;
	}
	
	function load_dpi_image($name) {
		$name = str_replace(".jpg","",$name);
		@list($u,$d) = @explode("_",$name);
		
		$decdate = @hexdec($d);
		return @imagecreatefromstring(load_file(IMAGE_DIR.'/images/'.@date("Y/F/d",$decdate)."/$name.jpg"));
	}
	
	
	function gd_flip_h(&$image) {
		$ret = gd_newimage(imagesx($image),imagesy($image));
		$w = imagesx($image);
		$h = imagesy($image);
		for ($x = 0; $x < $w; $x++) 
			imagecopy($ret, $image, $w - $x - 1, 0, $x, 0, 1, $h);
		
		return $ret;
	}
	function gd_flip_v(&$image) {
		$ret = gd_newimage(imagesx($image),imagesy($image));
		$w = imagesx($image);
		$h = imagesy($image);
		for ($x = 0; $x < $h; $x++) 
			imagecopy($ret, $image, 0, $h - $x - 1, 0, $x, $w, 1);
		return $ret;
	}	
	function gd_rotate(&$img,$ang=90,$col="FFFFFF") {
		$col = gd_hexgd($img,$col);
		return imagerotate($img,$ang,$col);
	}	
	
	function gd_3db_watermark(&$img,$filepath) {
		
		$w = imageSX($img);
		$h = imageSY($img);
		$img2 = imagecreatefromstring(load_file($filepath));
		$ww = imageSX($img2);
		$wh = imageSY($img2);
		$x = ($w-$ww)/2;
		$y = ($h-$wh)/2;
		
		imagecopy($img, $img2, $x, $y, 0, 0, $ww, $wh); 
		return $img;
	}
	function gd_set_bg(&$img,$color="FFFFFF") {
		$w = imageSX($img);
		$h = imageSY($img);
		$img2 = gd_newimage($w,$h);
		gd_bar($img2,0,0,$w,$h,$color);
		imagecopy($img2, $img, 0, 0, 0, 0, $w, $h); 
		return $img2;
	}

    function gd_hexgd(&$image,$hex) {
		$R = hexdec(@$hex{0}.@$hex{1});
		$G = hexdec(@$hex{2}.@$hex{3});
		$B = hexdec(@$hex{4}.@$hex{5});

		return imagecolorallocate ($image, $R, $G, $B);    
    }  
    function gd_bar(&$image,$x=0,$y=0,$width,$height,$color="FFFFFF") {
		return imagefilledrectangle ($image, $x, $y, $x+$width, $y+$height, gd_hexgd($image,$color));
    }

    function gd_text(&$image,$x,$y,$text,$color="000000",$font=2) {

		//return imagettftext ( $image, 14, 0, $x, $y, gd_hexgd($image,$color), "./arial.ttf", $text);

		return imagestring ($image, $font, $x, $y, $text, gd_hexgd($image,$color));	
    }
	function gd_loadfont($fname) {
		$f = "./templates/gdf/$fname.gdf";
		if(!file_exists($f)) {
			echo "file not found - $f<br />";
			return false;
		}
		$fn = @imageloadfont($f);
		if($fn === FALSE) return false;
		return $fn;
	}    
	function gd_textwidth($font=1,$text) {
		$ret= 0;
		if($text=='') return 0;
		return (imagefontwidth($font) * strlen($text));// - (imagefontwidth($font)/2);
	}
	function gd_textheight($font=1) {
		return imagefontheight($font);
	}	

    function gd_copy(&$image,$x,$y,$w,$h) {
		$ret=imagecreatetruecolor($w, $h); 
		imagecopy($ret,$image, 0, 0, $x, $y, $w, $h);    
		
		return $ret; 
    }
    function gd_paste(&$from,$to,$x=0,$y=0) {
		return imagecopymerge($to, $from, $x, $x, 0, 0, imagesx($from), imagesy($from), 100);      
    }

        
    function gd_newimage($width,$height) {
		return imagecreatetruecolor($width, $height); 
    }
    function gd_pixeltrans(&$image,$color) {
		return imagecolortransparent ($image, gd_hexgd($image,$color));    
    }
    
    function gd_line(&$image,$x,$y,$x2,$y2,$color="000000") {
		return imageline($image, $x,$y,$x2,$y2,gd_hexgd($image,$color));
    }
    function gd_rectangle(&$image,$x,$y,$w,$h,$color="000000") {
		$col = gd_hexgd($image,$color);
		
		imageline($image, $x,$y,$x,$y+$h,$col); //left
		imageline($image, $x,$y,$x+$w,$y,$col); //top
		imageline($image, $x+$w,$y,$x+$w,$y+$h,$col); //right
		imageline($image, $x,$y+$h,$x+$w,$y+$h,$col); //bottom
		
		return $image;
		
    }
    function gd_filledellipse(&$image,$x,$y,$w,$h,$color="ff0000") {
		$col = gd_hexgd($image,$color);
		imagefilledellipse ( $image, $x, $y, $w, $h, $col);
    }
    function gd_ellipse(&$image,$x,$y,$w,$h,$color="ff0000") {
		$col = gd_hexgd($image,$color);
		imageellipse ( $image, $x, $y, $w, $h, $col);
    }    
    function gd_loadimage($fname) { //ignore ext e.g. for xbm and xpm load jpg
		$arr = explode(".",$fname);
		$ext = strtolower($arr[count($arr)-1]);
		$func = 'imagecreatefrom';
		switch($ext) {
			case 'jpg' :
			case 'jpeg':
			case 'jpe' :  $func .= 'jpeg'; break;
			case 'png' :  $func .= 'png'; break;
			case 'gif' :  $func .= 'gif'; break;
			case 'xbm' :  $func .= 'xbm'; break;
			case 'wbmp':  $func .= 'wbmp'; break;
			
			/*case 'gif' :  $func .= 'jpeg'; break;
			case 'xbm' :  $func .= 'jpeg'; break;
			case 'xpm' :  $func .= 'jpeg'; break;
			case 'wbmp':  $func .= 'wbmp'; break;
			case 'bmp' :  $func .= 'jpeg'; break;
			case 'ico' :  $func .= 'png'; break;
			case 'cur' :  $func .= 'jpeg'; break;
			case 'ani' :  $func .= 'jpeg'; break;
			case 'txt' :  $func .= 'jpeg'; break;*/
			
		

		}
		if(!function_exists($func))
			return false;
		
		$res = @$func($fname);;
		if(!$res)
			$res = @imagecreatefromjpeg($fname);
		if(!$res) return false;
		
		return $res;
		
    }
    function gd_loadimage_orig($fname) {
		if(!file_exists($fname)) return false;
		
		$arr = explode(".",$fname);
		$ext = strtolower($arr[count($arr)-1]);

		if($ext=='jpe' or $ext == 'jpg') $ext = 'jpeg';
		
		$func = "imagecreatefrom$ext";			
				
		if(!function_exists($func)) 
			return false;

		//return $func($fname,$ext=='ico'?16:'',$ext=='ico'?32:100);
		return $func($fname);
		
    }    

    
    function write_jpeg(&$image,$fname,$quality=100) {
		if (function_exists("imagejpeg"))
			return @imagejpeg($image,$fname,$quality); 
		return false;
    }
    function write_png(&$image,$fname,$comp=9) {
		if (function_exists("imagejpng"))    
		if (function_exists("imagejpng"))    
			return @imagepng($image,$fname,$comp); 
		return false;			
    }    
    function write_gif(&$image,$fname) {
		if (function_exists("imagegif"))    
			return @imagegif($image,$fname); 
		return false;			
    }
Member Avatar for diafol

Firstly, have you contacted the author of the script to troubleshoot?
Secondly, do you really need all this functionality or are you just wanting to crop 15px off the thumbnail? If so, this script (or library!) is totally overkill.

To be honest, trawling through all that code is a pain. I'm afraid I don't have the time for all that. Sorry.

author of this script doesn't provide new features
what other functionality ? i just need crop Or tell php ignore 15 pixel from bottom when it capture the original image

Member Avatar for diafol

author of this script doesn't provide new features
what other functionality ? i just need crop Or tell php ignore 15 pixel from bottom when it capture the original image

This is what I was saying!
Your script is way too complicated for just a simple thumb crop. It's great, don't get me wrong, but a little heavy-handed for what you need.

If you tell me what dimensions you want for the displayed thumbnail, perhaps I can modify my previous suggestion to fit your needs. And - does the thumbnail need to be in proportion.

dimension for thumbnail could be height= 150 px ,,, and width = 150 , but there is different sizes for images, can it preserve aspect ratio ? if not ok no problem , and if we can make it in proportion it would be great , if cannot , it's ok

here is an example of image and watermark http://img683.imageshack.us/img683/2827/example.gif

Member Avatar for diafol

dimension for thumbnail could be height= 150 px ,,, and width = 150 , but there is different sizes for images, can it preserve aspect ratio ? if not ok no problem , and if we can make it in proportion it would be great , if cannot , it's ok

here is an example of image and watermark http://img683.imageshack.us/img683/2827/example.gif

OK F, will have a look later on - got to pick up the kids. Hope to get back in a couple of hours.

BTW: anybody else out there? This ain't a private discussion. All solutions considered, especially as I only flirt with GD.

Member Avatar for diafol

Like my previous solution:

<?php
function CutThumb($img){
//set variables
	$crop = 15;
	$thumb_w = 150;
	$thumb_h = 150;
//get info from original image
 	list($w, $h) = getimagesize($img);
//make image a resource for use in functions	   
        $img_r = imagecreatefromjpeg($img);
//make image container for functions	
	$thumb = imagecreatetruecolor($thumb_w,$thumb_h);
//crop and resize the original image 
	imagecopyresampled($thumb,$img_r,0,0,0,0,$thumb_w,$thumb_h,$w,$h-$crop);
//release memory
        imagedestroy($img_r);
//return image    
	return $thumb;
}
 
//Create the thumbnail - this could be in any file if the function above is included
$MyCutThumb = CutThumb("burton.jpg");
// Display image
header('Content-type: image/jpeg');
imagejpeg($MyCutThumb);
?>

Managed to cut out a few unnecessary steps too.
This does not keep image in proportion, but easy to do. Do you want to proportion by:

1) Keeping whole image in thumbnail, but with black (or coloured) bands for areas less than 150px. like a normal TV showing a widescreen film. If so, do we place bands at either side of the image or on the top OR bottom / left OR right?

2) Does the image get further cropped - if so, take off the top OR bottom OR left OR right or equal amount from top AND bottom OR left AND right?


Thinking about it, you could make it a proper function with "settable" variables:

function CutThumb($img,$crop=15,$thumb_w=150,$thumb_h=150){
 	list($w, $h) = getimagesize($img);
        $img_r = imagecreatefromjpeg($img);
	$thumb = imagecreatetruecolor($thumb_w,$thumb_h);
	imagecopyresampled($thumb,$img_r,0,0,0,0,$thumb_w,$thumb_h,$w,$h-$crop);
        imagedestroy($img_r);
	return $thumb;
}
 
//overrides default values - creates 100x100px thumb with 25px taken off bottom of orig. image.
$MyCutThumb = CutThumb("burton.jpg",25,100,100);

Thanks you so much Dear ardav
it's solved
i love this forum

Greetings & Respect

Member Avatar for diafol

Croeso.

Here's the modified script with proportion (constraints):

<?php
function CutThumb($img,$crop=15,$thumb_w=150,$thumb_h=150){
//get info from original image
 	list($w, $h) = getimagesize($img);

//check dimensions
	$prop_h = $thumb_h;
	$prop_w = $thumb_w;
	$prop_x = 0;
	$prop_y = 0;
	if($w > $h){
		$prop_h = intval($h * $thumb_h/$w);
		$prop_y = intval(($thumb_h - $prop_h)/2);
	}elseif($h > $w){
		$prop_w = intval($w * $thumb_w/$h);
		$prop_x = intval(($thumb_w - $prop_w)/2);
	}

        $img_r = imagecreatefromjpeg($img);
	$thumb = imagecreatetruecolor($thumb_w,$thumb_h);
	imagecopyresampled($thumb,$img_r,$prop_x,$prop_y,0,0,$prop_w,$prop_h,$w,$h-$crop);
        imagedestroy($img_r);
	return $thumb;
}
 
//Create the thumbnail - this could be in any file if the function above is included
$MyCutThumb = CutThumb("map.jpg",25,100,100);
// Display image
header('Content-type: image/jpeg');
imagejpeg($MyCutThumb);
?>

I've checked it and it produces equal bands on top/bottom OR left/right, depending on which side (height OR width) is greatest.

Great one Dear
Really Thank you so much, you helped me alot

Greetings and Respect

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.