Jump to content

Image upload crop script


jimmyoneshot

Recommended Posts

I've been creating my own image upload script which takes in several parameters to style an image, however when the function is used new width and new height are specified but I need to use these values to determine cropping coordinates.

 

What I want is when a new width and height is specified, if it is squared i.e. new width is equal to new height AND the original image is not squared i.e. it is a vertical or horizontal rectangle then the central part of that image will be taken.

 

So for example if the user uploads an image 500px high and 100px wide and sets the new width and height both at 100px the new image should be a 100px by 100px square which is taken 200px from the top of the original image.

 

Here is the functions I currently use so far. It's just the coordinates function I can't seem to figure out. Can anyone please help me out:-

 


//gets the extension of an uploaded file
function getExtension($file) {
$i = strrpos($file,".");//Gets the position of the "." in the filename
if (!$i) { return ""; }//If there is no "." the function ends and returns ""      
$l = strlen($file) - $i;
$ext = substr($file,$i+1,$l);
$ext = strtolower($ext);	
return $ext;
}

//checks image dimensions and scales both sizes by ratio if it exceeds the max
//$w = width, $mw = max width, $h = height, $mh = max height,
function checkSize($w, $mw, $h, $mh){

if($w > $mw){//Checks if width is greater than max width			
	$ratio = $mw / $w;
	$h = $h * $ratio;
	$h = round($h);
	$w = $mw;						
}

if($h > $mh){//Checks if height is greater than max height		
	$ratio = $mh / $h;
	$w = $w * $ratio;
	$w = round($w);	
	$h = $mh;													
}

return array($w, $h);

}

//Used to get the coordinates to resize an image by
function getCoords(){


}

//$f = the file, $ext = file extension, $nw = new width, $nh = new height, $mw = max width, $mh = max height, $nf = new filename, $fo = folder, $des = file size description, $q = quality
function imageUpload($f, $ext, $nw, $nh, $mw, $mh, $nf, $fo, $des, $q){

//create image from uploaded file type
if($ext=="jpg" || $ext=="jpeg" ){
	$src = imagecreatefromjpeg($f);			
}else if($ext=="png"){
	$src = imagecreatefrompng($f);
}else{
	$src = imagecreatefromgif($f);
}

//creates a list of the width and height of the image
list($w,$h)=getimagesize($f);

//sets the coordinates for resizing to 0 by default
$dx = $dy = $sx = $sy = 0;

//if new width and height are both 0 then a resize is not required so original dimensions need to be validated in case they exceed their max
if($nw == 0 && $nh == 0){

	if($w > $mw  || $h > $mh){//checks if width or height are greater than their max 

		list($w, $h) = checkSize($w, $mw, $h, $mh);

	}

	$nw = $w;
	$nh = $h;

}else if($nw == $nh && $w !== $h){//this is for if the resized image needs to be squared but the original image is not a square

	//COORDS FUNCTION NEEDED HERE

}

$desext = "";//sets the description extension to "" by default	
if($des !== 0){//Checks if $des is set or not
	$desext .= "_".$des;//appends des to $desext ready to be appended to the filename
}

$foext = "";//sets the folder extension to "" by default
if($fo !== 0){//Checks if $fo is set or not
	$foext .= $fo."/";//appends folder to $foext ready to be appended to the filename
}

$qv = 100;//sets the quality value to 100 percent by default
if($q !== 0){//Checks if $q is set or not
	$qv .= $q;//sets the quality value to the passed value
}

$tmp=imagecreatetruecolor($nw,$nh);	
imagecopyresampled($tmp,$src,$dx,$dy,$sx,$sy,$nw,$nh,$w,$h);
$fn = "images/".$foext.$nf.$desext.".jpg";//sets the final filename for upload
imagejpeg($tmp,$fn,$qv);//uploads the file

//empty variables and clear image
imagedestroy($src);
imagedestroy($tmp);

}

Link to comment
Share on other sites

Managed to figure this out using the above posted article. Thanks again.

 

Here's the code I ended up with. If anyone can suggest improvements or alterations I'd be grateful as it's a work in progress which I want to be able to include at the top of every page which requires an image upload to simplify the process.

 

I would like to be able to advance the function a little so that if the new image is squared it uploads several versions of the same produced image i.e. a small 50 by 50 version and a tiny 25 by 25 version of the cropped image

 

:-

 


//gets the extension of an uploaded file
function getExtension($file) {
$i = strrpos($file,".");//Gets the position of the "." in the filename
if (!$i) { return ""; }//If there is no "." the function ends and returns ""      
$l = strlen($file) - $i;
$ext = substr($file,$i+1,$l);
$ext = strtolower($ext);	
return $ext;
}

function validateImage($f){

//Sets error to 0 by default
$e = 0;

if($f['size'] == 0){//checks the file actually exists i.e. it's size is not 0
	$e = 1;
}else if($f['size'] >= 1048576){//checks the file size is not too large 
	$e = 2;
}else{

	$ext = getExtension($f['name']);

	if (($ext != "jpg") && ($ext != "jpeg") && ($ext != "png") && ($ext != "gif")){//checks the file has a valid extension
		$e = 3;
	}

}

return $e;

}

//checks image dimensions and scales both sizes by ratio if it exceeds the max
//$w = width, $mw = max width, $h = height, $mh = max height,
function checkSize($w, $mw, $h, $mh){

if($w > $mw){//Checks if width is greater than max width			
	$ratio = $mw / $w;
	$h = $h * $ratio;
	$h = round($h);
	$w = $mw;						
}

if($h > $mh){//Checks if height is greater than max height		
	$ratio = $mh / $h;
	$w = $w * $ratio;
	$w = round($w);	
	$h = $mh;													
}

return array($w, $h);

}

//Used to get the coordinates to resize an image by
function getCoords($w, $nw, $h, $nh){

$x = ($w - $nw)/2;
$y = ($h - $nh)/2;

return array($x,$y);

}

//$f = the file, $ext = file extension, $nw = new width, $nh = new height, $mw = max width, $mh = max height, $nf = new filename, $fo = folder, $des = file size description, $q = quality
function imageUpload($f, $ext, $nw, $nh, $mw, $mh, $nf, $fo, $des, $q){

//create image from uploaded file type
if($ext=="jpg" || $ext=="jpeg" ){
	$src = imagecreatefromjpeg($f);			
}else if($ext=="png"){
	$src = imagecreatefrompng($f);
}else{
	$src = imagecreatefromgif($f);
}

//creates a list of the width and height of the image
list($w,$h)=getimagesize($f);

//sets the coordinates for resizing to 0 by default
$x = $y = 0;

//if new width and height are both 0 then a resize is not required so original dimensions need to be validated in case they exceed their max
if($nw == 0 && $nh == 0){

	if($w > $mw  || $h > $mh){//checks if width or height are greater than their max 

		list($w, $h) = checkSize($w, $mw, $h, $mh);//returns the resized width and height from the checksize function

	}

	$nw = $fw = $w;//sets the final width and the new width to the original width
	$nh = $fh = $h;//sets the final height and the new height to the original height

}else if($nw == $nh && $w !== $h){//this is for if the resized image needs to be squared but the original image is not a square

	list($x, $y) = getCoords($w, $nw, $h, $nh);

	$fw = $w;//sets the final width to the original width
	$fh = $h;//sets the final height to the original height

}

$desext = "";//sets the description extension to "" by default	
if($des !== 0){//Checks if $des is set or not
	$desext .= $des;//appends des to $desext ready to be appended to the filename
}

$foext = "";//sets the folder extension to "" by default
if($fo !== 0){//Checks if $fo is set or not
	$foext .= $fo;//appends folder to $foext ready to be appended to the filename
}

$qv = 100;//sets the quality value to 100 percent by default
if($q !== 0){//Checks if $q is set or not
	$qv .= $q;//sets the quality value to the passed value
}

$can=imagecreatetruecolor($nw,$nh);//creates the canvas for the image
imagecopyresampled($can,$src,0,0,$x,$y,$fw,$fh,$w,$h);
$fn = "images/".$foext.$nf.$desext.".jpg";//sets the final filename for upload

if( file_exists($fn) ){//checks if the file being uploaded already exists
	unlink($fn);//deletes the file if it already exists
}

imagejpeg($can,$fn,$qv);//uploads the file

//empty variables and clear image
imagedestroy($src);
imagedestroy($can);

}

Link to comment
Share on other sites

One thing I now need to be able to do is return the file that was created by my above crop script so that it can then be uploaded as a thumbnail version of that image.

 

So basically I need to :-

 

1. Take in an image which is rectangular but allows the central squared part of that rectangle to be cropped to and then uploaded (DONE)

2. Upload the file and then return the file that was created so that it can be used in a subsequent upload to create a thumbnail version of it (NOT DONE)

 

How can I work this into my script?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.