Jump to content

[SOLVED] Help needed with Cropping


theCro

Recommended Posts

//FULL SERVER PATH TO THIS FILE -- REPLACE WITH YOUR INFO (ENTIRE PATH MAY DIFFER)

$server_path = "/home/zadarsp/public_html";

 

//PATH TO IMAGES DIRECTORY

$img_path = "foto/";

 

 

//CONVERT THE IMAGE

//PATH TO IMAGEMAGICK: /usr/X11R6/bin/convert (MAY BE DIFFERENT ON YOUR SERVER - REPLACE IF NECESSARY)

$tmpMakeIMG = "/usr/local/bin/convert -density 300X300 -quality 100 $geometry $img_crop -colorspace RGB $server_path$img_path$img_start $server_path$img_path$img_end";

$makeIMG = `$tmpMakeIMG`;

 

 

this is what is in ... its all correct!

Link to comment
Share on other sites

Can you post the entire script for foto.php.

 

If you enter the following URL http://www.zadarsport.com/foto.php?img_start=abc123.jpg&img_end=new.jpg&w=300&h=200&crop=yes, (assuming abc123.jpg doesn't exist on your server) you should at the very least get the following error:

 

ERROR: abc123.jpg file not found! Verify the server_path and img_path variables.

 

Can you also set error reporting to E_ALL at the top of the script for testing:

error_reporting(E_ALL);

Link to comment
Share on other sites

Can you post the entire script for foto.php.

 

If you enter the following URL http://www.zadarsport.com/foto.php?img_start=abc123.jpg&img_end=new.jpg&w=300&h=200&crop=yes, (assuming abc123.jpg doesn't exist on your server) you should at the very least get the following error:

 

ERROR: abc123.jpg file not found! Verify the server_path and img_path variables.

 

Can you also set error reporting to E_ALL at the top of the script for testing:

error_reporting(E_ALL);

,,

there u go

<?PHP

//CURRENT IMG NAME; DESIRED IMG NAME (OPTIONAL); RESIZE WIDTH (OPTIONAL); RESIZE HEIGHT (OPTIONAL); DESIRED IMG FORMAT (jpg, png, etc.; OPTIONAL); CROP (yes; OPTIONAL)
//IF CROPPING IS NOT SET TO yes, WILL RESIZE PROPORTIONALLY TO FIT USING THE WIDTH AND HEIGHT AS MAXIMUMS (IF PROVIDED)
//DESIRED IMG NAME WILL OVERRIDE THE DESIRED IMG FORMAT e.g., original.jpg > new.jpg with format set to png WILL CREATE A JPG FILE CALLED new.jpg

function convert_img ($img_start, $img_end, $width, $height, $format, $crop) {

//FULL SERVER PATH TO THIS FILE -- REPLACE WITH YOUR INFO (ENTIRE PATH MAY DIFFER)
$server_path = "/home/zadarsp/public_html";

//PATH TO IMAGES DIRECTORY
$img_path = "foto/";

//CHECK IF ORIGINAL FILE EXISTS
if (is_file($server_path.$img_path.$img_start)) {

	//CHECK IF DESIRED IMG NAME IS SPECIFIED
	if (!$img_end){

		//OTHERWISE CHECK IF FORMAT IS SPECIFIED - FORMAT WILL ONLY CHANGE IF IMG_END IS NOT SPECIFIED
		if ($format){
			$img_arr = explode(".", $img_start);
			$img_end = $img_arr[0].".".$format;
		} else {
			$img_end = $img_start;
		}

	}

	//CHECK IF RESIZE WIDTH & RESIZE HEIGHT WERE PROVIDED; ADJUST ACCORDINGLY
	if (!$width && !$height){
		$geometry = "";
	} else {
		if (!$height) {
			$geometry = "-geometry ".$width;
		} else if (!$width) {
			$geometry = "-geometry X".$height;	
		} else {
			$geometry = "-geometry ".$width."X".$height;
		}
	}

	//CHECK IF CROPPING IS REQUESTED - REQUIRES HEIGHT AND WIDTH TO BE SPECIFIED
	if ($crop == "yes"){
		if (!$width || !$height){
			return $error = "ERROR: Specify a width and height or set crop to 'no'";
		}

		$img_size = getimagesize($img_path.$img_start);

		//CALCULATES ASPECT RATIO TO DETERMINE HOW TO RESIZE THE IMAGE PROPORTIONALLY TO MINIMIZE CROPPING
		if(($img_size[0]/$img_size[1]) >= ($width/$height)) {
			$geometry = "-geometry X".$height;
		} else {
			$geometry = "-geometry ".$width;
		}

		//CURRENTLY CROPS FROM THE CENTER OF THE IMAGE -- TRIMMING EQUAL AMOUNTS (ONLY IF NECESSARY) ON THE TOP/BOTTOM; LEFT/RIGHT
		//CROPPING OPTIONS FOR THE GRAVITY INCLUDE: NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast (CHANGE IF NECESSARY)
		$img_crop = "-gravity Center -crop ".$width."X".$height."+0+0";
	} else {
		$img_crop = "";
	}

	//CONVERT THE IMAGE
	//PATH TO IMAGEMAGICK: /usr/X11R6/bin/convert (MAY BE DIFFERENT ON YOUR SERVER - REPLACE IF NECESSARY)
	$tmpMakeIMG = "/usr/local/bin/convert -density 300X300 -quality 100 $geometry $img_crop -colorspace RGB $server_path$img_path$img_start $server_path$img_path$img_end";
	$makeIMG = `$tmpMakeIMG`;

	//DISPLAY THE IMAGE OR RETURN ERROR MESSAGE; USE FOR TESTING; DELETE THIS CODE IF NOT REQUIRED
	if (is_file($server_path.$img_path.$img_end)) {

		$img_size = getimagesize($img_path.$img_end);

		return $img = '<IMG SRC="'.$img_path.$img_end.'" border="0" '.$img_size[3].'>';
	} else {
		return $error = "ERROR: There was a problem converting the image!";
	}


} else {
	return $error = "ERROR: $img_start file not found! Verify the server_path and img_path variables.";
}

}


//EXAMPLE USAGE -- ONLY UN-COMMENT THE ONE YOU WANT TO USE:

//OUTPUTS A FILE THAT IS PROPORTIONALLY RESIZED AND CROPPED TO 200px by 200px CALLED new.jpg
//print convert_img ("original.jpg", "new.jpg", "200", "200", "", "yes");

//OUTPUTS A FILE THAT IS PROPORTIONALLY RESIZED TO EITHER 200px by X -OR- X by 200px (DEPENDING ON THE ASPECT RATIO) CALLED new.jpg
//print convert_img ("original.jpg", "new.jpg", "200", "200", "", "");

//OUTPUTS A FILE THAT IS PROPORTIONALLY RESIZED TO 200px by X CALLED original.png
//print convert_img ("original.jpg", "", "200", "", "png", "");

//OUTPUTS A FILE THAT IS PROPORTIONALLY RESIZED TO 200px by X CALLED original.png (alternate way of accomplishing the above)
//print convert_img ("original.jpg", "original.png", "200", "", "", "");

//OVERWRITES THE ORIGINAL FILE AND RESIZES TO X by 200px
//print convert_img ("original.jpg", "", "", "200", "", "");


?>

Link to comment
Share on other sites

You never added the code that calls the function (and all the lines at the bottom are commented out)??? You either have to add the following code to pass vars via a URL or un-comment one of the lines at the bottom.

 

print convert_img ($_GET['img_start'], $_GET['img_end'], $_GET['w'], $_GET['h'], "", $_GET['crop']);

 

Drop it in right above the last line of code and try again.

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.