Jump to content

Resize and crop images, rectangle not square thumbs


braiiins

Recommended Posts

I've got a script I'm using to resize and crop images into square thumbnails, and it works great. I want to, however, use this script to do the same thing, but to create thumbnails that are rectangular, specifically 465px width x 275px height. As it is now, it creates 465x465 images.

 

Here is the script -

 

<?php
class cropImage{
var $imgSrc,$myImage,$cropHeight,$cropWidth,$x,$y,$thumb;

function setImage($image){
	//Your Image
  		 $this->imgSrc = $image; 
	//getting the image dimensions
  		list($width, $height) = getimagesize($this->imgSrc); 
	//create image from the jpeg
	$this->myImage = imagecreatefromjpeg($this->imgSrc) or die("Error: Cannot find image!"); 
	if($width > $height){
		$biggestSide = $width; //find biggest length
		}
	else{
		$biggestSide = $height; 
		}
                     
	//The crop size will be half that of the largest side 
  		$cropPercent = .5; // This will zoom in to 50% zoom (crop)
	$this->cropWidth   = $biggestSide*$cropPercent; 
	$this->cropHeight  = $biggestSide*$cropPercent; 
                     
                     
	//getting the top left coordinate
	$this->x = ($width-$this->cropWidth)/2;
	$this->y = ($height-$this->cropHeight)/2;
	}  

function createThumb(){
	$thumbSize = 465;
	$this->thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
	imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight); 
	}

function renderImage(){
	header('Content-type: image/jpeg');
	imagejpeg($this->thumb);
	imagedestroy($this->thumb); 
	}  
}
$src= $_GET['src'];
$image = new cropImage();
$image->setImage($src);
$image->createThumb();
$image->renderImage();


?>

 

I've tried a number of changes, but to be honest, I don't really know what I'm doing and was just trial and erroring. Any thoughts?

 

Also, while I'm here, how would I change the code to allow cropping of gifs as well as jpgs?

Link to comment
Share on other sites

Take a look at the following code:

function createThumb(){
  $thumbSize = 465;
  $this->thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight); 
}

 

Note that $thumbSize will be repeated twice, So try this:

function createThumb(){
  $thumbSize_x = 465;
  $thumbSize_y = 275;
  $this->thumb = imagecreatetruecolor($thumbSize_x, $thumbSize_y); 
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize_x, $thumbSize_y, $this->cropWidth, $this->cropHeight); 
}

Link to comment
Share on other sites

So I was able to figure it out to create the proper aspect ratio for images with a horizontal layout, but images with a vertical layout with out of ratio still. I decided to try an if statement, and it worked! Here is the code for anyone else who might want to use it ($biggestSide instances no longer needed so I removed them) -

 

<?php
class cropImage{
var $imgSrc,$myImage,$cropHeight,$cropWidth,$x,$y,$thumb;

function setImage($image){
	//Your Image
  		 $this->imgSrc = $image; 
	//getting the image dimensions
  		list($width, $height) = getimagesize($this->imgSrc); 
	//create image from the jpeg
	$this->myImage = imagecreatefromjpeg($this->imgSrc) or die("Error: Cannot find image!"); 
	if($width > $height){
		$biggestSide = $width; //find biggest length
		}
	else{
		$biggestSide = $height; 
		}

	if($height > $width){
		$cropPercent = .5; // This will zoom in to 50% zoom (crop)
		$this->cropWidth   = $width; 
		$this->cropHeight  = $height*$cropPercent; 
		}
	else{
		$cropPercent = .5; // This will zoom in to 50% zoom (crop)
		$this->cropWidth   = $width*$cropPercent; 
		$this->cropHeight  = $height*$cropPercent; 
		}
                     
                     
	//getting the top left coordinate
	$this->x = ($width-$this->cropWidth)/2;
	$this->y = ($height-$this->cropHeight)/2;
	}  

function createThumb(){
  		$thumbSize_x = 459;
  		$thumbSize_y = 275;
  		$this->thumb = imagecreatetruecolor($thumbSize_x, $thumbSize_y); 
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize_x, $thumbSize_y, $this->cropWidth, $this->cropHeight); 
}

function renderImage(){
	header('Content-type: image/jpeg');
	imagejpeg($this->thumb);
	imagedestroy($this->thumb); 
	}  
}
$src= $_GET['src'];
$image = new cropImage();
$image->setImage($src);
$image->createThumb();
$image->renderImage();


?>

 

 

Still not sure how to get it working to be able to crop gif images, would appreciate some help with this one!

Link to comment
Share on other sites

I should mention - I would like to edit the script so that it will crop and resize BOTH jpg and gif files, not just gif files. I know it works for gif files alone, as I adjusted it and tested it, I just can't figure out how to get it to do both.

Link to comment
Share on other sites

teamatomic, I hate having to ask, but I'm trying and can't figure out how to set it up... I searched for how to use the explode function and tried what I could find, with the if statements, and I just can't get it to work... Driving me crazy!

Link to comment
Share on other sites

So I'm pretty sure I know why it's not working (what teamatomic said to do) - The image is being called from the script like this -

 

<img class="thumbnail" src="/gallery/thumbcreate2.php?src=<?php echo get_post_meta($post->ID, 'full-image', $single = true) ?>" alt="<?php echo the_title() ?>" width="459" height="275" />

 

Is there any way around this to either pull the image extension or another way to tell the script to also work with gif files without having to pull the extension?

Link to comment
Share on other sites

So I'm pretty sure I know why it's not working (what teamatomic said to do) - The image is being called from the script like this -

 

<img class="thumbnail" src="/gallery/thumbcreate2.php?src=<?php echo get_post_meta($post->ID, 'full-image', $single = true) ?>" alt="<?php echo the_title() ?>" width="459" height="275" />

 

echo get_post_meta($post->ID, 'full-image', $single = true)

 

Why not get the extension from there?

$ext = end(explode('.', get_post_meta($post->ID, 'full-image', $single = true)));

 

Link to comment
Share on other sites

Gave that a shot oni-kun... images still not loading, so I viewed the site source, and it did work to add the extension... except the extension was already there, so it looked something like this (removed domain info, paths are full in the source)

 

<img class="thumbnail" src="/gallery/thumbcreate2.php?src=/wp-content/uploads/2010/03/image.gifgif" alt="post" width="459" height="275" />

 

So without the code oni-kun gave me, the source code is showing the images loading properly, except they aren't actually loading with the explode function / if statements. I'm sure I screwed it up somewhere, I'm just piecing things together from searching the internet, not much PHP knowledge (ok, none, except for what I see online)! Hoping someone can find my mistake here because if not I'm thinking about calling it quits!... Here is the script -

 

<?php
class cropImage{
var $imgSrc,$myImage,$cropHeight,$cropWidth,$x,$y,$thumb;

function setImage($image){
	//Your Image
  		 $this->imgSrc = $image; 
  		 $ext = end(explode('.', $image));
	//getting the image dimensions
  		list($width, $height) = getimagesize($this->imgSrc); 
	//create image
	if($ext = jpeg || $ext = jpg){
		$this->myImage = imagecreatefromjpeg($this->imgSrc);
		}
	if($ext = gif){
		$this->myImage = imagecreatefromgif($this->imgSrc);
		} 

	if($height > $width){
		$cropPercent = .5; // This will zoom in to 50% zoom (crop)
		$this->cropWidth   = $width; 
		$this->cropHeight  = $height*$cropPercent; 
		}
	else{
		$cropPercent = .5; // This will zoom in to 50% zoom (crop)
		$this->cropWidth   = $width*$cropPercent; 
		$this->cropHeight  = $height*$cropPercent; 
		}
                     
                     
	//getting the top left coordinate
	$this->x = ($width-$this->cropWidth)/2;
	$this->y = ($height-$this->cropHeight)/2;
	}  

function createThumb(){
  		$thumbSize_x = 459;
  		$thumbSize_y = 275;
  		$this->thumb = imagecreatetruecolor($thumbSize_x, $thumbSize_y); 
	imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize_x, $thumbSize_y, $this->cropWidth, $this->cropHeight); 
	}

if ($ext = jpeg || $ext = jpg) {
	function renderImage(){
		header('Content-type: image/jpeg');
		imagejpeg($this->thumb);
		imagedestroy($this->thumb); 
		}  
	}

if ($ext = gif) {
	function renderImage(){
		header('Content-type: image/gif');
		imagegif($this->thumb);
		imagedestroy($this->thumb); 
		} 
	}

$src= $_GET['src'];
$image = new cropImage();
$image->setImage($src);
$image->createThumb();
$image->renderImage();

?>

 

and this is the code in the file to show the image -

 

<img class="thumbnail" src="/gallery/thumbcreate.php?src=<?php echo get_post_meta($post->ID, 'full-image', $single = true) ?>" alt="<?php echo the_title() ?>" width="459" height="275" />

 

And I wanted to thank you guys for the help so far! Sorry for being such a pain :)

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.