Image Cropping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
martinm_92
Forum Newbie
Posts: 2
Joined: Wed Sep 07, 2011 7:34 am

Image Cropping

Post by martinm_92 »

Hey guys, I have this function for resizing images that has worked for me fine up till now, I want it be able to crop images to certain sizes mainly for thumbnails etc. I wondered if anyone could take a look? I have been on the net looking for hours now trying to learn how to do it but it keeps going wrong...

Code: Select all

<?php
function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { 
	$s_path = trim($s_path); 
	$o_path = trim($o_path); 
	$save = $s_path . $save; 
	$file = $o_path . $file; 
	$ext = strtolower(end(explode('.',$save))); 
	list($width, $height) = getimagesize($file) ; 
	if(($width>$t_w) OR ($height>$t_h)) { 
		$r1 = $t_w/$width; 
		$r2 = $t_h/$height; 
		if($r1<$r2) { 
			$size = $t_w/$width; 
		}else{ 
			$size = $t_h/$height; 
		} 
	}else{ 
		$size=1; 
	} 
	$modwidth = $width * $size; 
	$modheight = $height * $size; 
	$tn = imagecreatetruecolor($modwidth, $modheight) ; 
	switch ($ext) { 
		case 'jpg': 
		case 'jpeg': 
			$image = imagecreatefromjpeg($file) ; 
			break; 
		case 'gif': 
			$image = imagecreatefromgif($file) ; 
			break; 
		case 'png': 
			$image = imagecreatefrompng($file) ; 
			break; 
	} 
	imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
	imagejpeg($tn, $save, 100) ; 
	return; 
}

$file = $newfile;
$filename = $dir . $file;

/* Photoviewer Picture */
$save = $filename;
$t_w = 590;
$t_h = 347;
$o_path = "upload/"; 
$s_path = "upload/newfile/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

/* Profile Picture */
$t_w = 380;
$t_h = 137;
$o_path = "upload/newfile/"; 
$s_path = "upload/profpic/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

/* Photogallery Picture */
$t_w = 130;
$t_h = 130;
$o_path = "upload/newfile/"; 
$s_path = "upload/photogallery/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

/* Thumbnail Picture */
$t_w = 50;
$t_h = 50;
$o_path = "upload/newfile/"; 
$s_path = "upload/thumb/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);
?>
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: Image Cropping

Post by greip »

You might get more response if you describe in what way this function does not work.

Have you tried inserting "echo" or "error_log()" statements in the code to verify that the variables contain the expected values at the various parts of the function?
martinm_92
Forum Newbie
Posts: 2
Joined: Wed Sep 07, 2011 7:34 am

Re: Image Cropping

Post by martinm_92 »

The function works find as it is. It re sizes the images perfectly however I want to be able to crop the images to certain sizes instead of it re sizing them to the photos specific ratio. For example having a picture sized 540 x 370 I want it cropped to 100 x 100? Is that clear? I just read that back and I think it makes sense? Haha...
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Image Cropping

Post by litebearer »

Might take a look here (arguably not the best; however, it may help)...
http://www.nstoia.com/sat/crop/
Post Reply