Image saving issue

A

Anonymous

Guest
If you want 300dpi then you need 300 pixels per inch.

So
Code:
<?php
function resizeimage( $filename, $savename, $width, $height, $dpi )
{
$size_w = $width*$dpi;
$size_h = $height*$dpi;

$image = imagecreatetruecolor( $size_w, $size_h );
$info = getimagesize( $filename );
$import = imagecreatefromjpeg( $filename );
imagecopyresampled( $image, $import, 0, 0, 0, 0, $size_w, $size_h, $info[0], $info[1] );
imagepng( $image, $savename, 0 );
imagedestroy( $image );
}

// Set the dpi
$dpi = 300;

// 24" x 18"
$width = 24; // In Inches
$height = 18; // In Inches
resizeimage( $filename, $savename, $width, $height, $dpi );
?>

One thing to note this code does not scale the image and add borders.
It only resizes by stretching.
 
Hello,

Try Below Code Function:

Code:
function createthumb($tmp_name, $file_name, $upload_path ="", $prefix = "", $img_id = "", $new_w = "23", $new_h = "15")
{
	//$src_img=imagecreatefromjpeg($name);
	$filetype = substr($file_name,strlen($file_name)-4,4);
	$filetype = strtolower($filetype);
	
	if($filetype == ".gif")  $src_img = @imagecreatefromgif($tmp_name);  
	if($filetype == ".jpg")  $src_img = @imagecreatefromjpeg($tmp_name);  
	if($filetype == ".png")  $src_img = @imagecreatefrompng($tmp_name);  
	
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	
	if ($old_x > $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;$old_y*($new_h/$old_x);
	}
	
	if ($old_x < $old_y) 
	{
		$thumb_w=$new_w;$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	
	if ($old_x == $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	
	$watermark = imagecreatefrompng(UPLOAD_FILES.'watermark.png');
	$watermarkwidth =  imagesx($watermark);
	$watermarkheight =  imagesy($watermark);
	$startwidth = (($imagewidth - $watermarkwidth)/2);
	$startheight = (($imageheight - $watermarkheight)/2);
	
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
	#imagecopyresampled($dst_img, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
	imagecopy($dst_img, $watermark,  $startwidth+100, $startheight+100, 0, 0, $watermarkwidth, $watermarkheight);
	
	if($filetype == ".gif")
		imagegif($dst_img, $upload_path.$prefix.$img_id.$filetype); 
	
	if($filetype == ".jpg")
		imagejpeg($dst_img, $upload_path.$prefix.$img_id.$filetype); 
		
	if($filetype == ".png")
		imagepng($dst_img, $upload_path.$prefix.$img_id.$filetype); 
	
	imagedestroy($dst_img);
	imagedestroy($src_img);
	
	return $prefix.$img_id.$filetype;
}

http://www.mukeshvariya.com
 
Back
Top