This script will allow you to make a smaller image of a bigger image, but at the same time crop it. This will prevent the image looking stretched and deformed.
We will create this using a class (Sorry using PHP 4 at the moment, but should work on 5)
Firstly lets set up the class.
Firstly lets set up the class.
PHP Code: class cropImage{
//code here
}
Now we need to set up some variables to be used throughout the program.
//code here
}
Now we need to set up some variables to be used throughout the program.
PHP Code: var $imgSrc,$myImage,$cropHeight,$cropWidth,$x,$y,$thumb;
The variables above will be explained once we use them. Now we need to create the first function. This function will get the image we are going to crop, work out its dimension and then use them dimensions to work out how we are going to crop it.
The variables above will be explained once we use them. Now we need to create the first function. This function will get the image we are going to crop, work out its dimension and then use them dimensions to work out how we are going to crop it.
PHP Code:
function setImage($image)
{
function setImage($image)
{
//Your Image
$this->imgSrc = $image;
$this->imgSrc = $image;
//getting the image dimensions
list($width, $height) = getimagesize($this->imgSrc);
list($width, $height) = getimagesize($this->imgSrc);
//create image from the jpeg
this->myImage = imagecreatefromjpeg($this->imgSrc) or die("Error: Cannot find image!");
this->myImage = imagecreatefromjpeg($this->imgSrc) or die("Error: Cannot find image!");
if($width > $height) $biggestSide = $width; //find biggest length
else $biggestSide = $height;
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;
$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;
$this->x = ($width-$this->cropWidth)/2;
$this->y = ($height-$this->cropHeight)/2;
}
Now we actually need to start creating the actual cropped image.
Now we actually need to start creating the actual cropped image.
PHP Code: function createThumb()
{
{
$thumbSize = 250; // will create a 250 x 250 thumb
$this->thumb = imagecreatetruecolor($thumbSize, $thumbSize);
$this->thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight);
}
Now all we need to do is render the image out.
}
Now all we need to do is render the image out.
PHP Code: function renderImage()
{
{
header('Content-type: image/jpeg');
imagejpeg($this->thumb);
imagedestroy($this->thumb);
}
Now we just need to create the instance
imagejpeg($this->thumb);
imagedestroy($this->thumb);
}
Now we just need to create the instance
PHP Code: $image = new cropImage;
$image->setImage($src);
$image->createThumb();
$image->renderImage();
You can use this script to display a thumb of images on a new page, by using the following page.
$image->setImage($src);
$image->createThumb();
$image->renderImage();
You can use this script to display a thumb of images on a new page, by using the following page.
PHP Code: <img src="thumbcreate.php?src=images/largimg.jpg"> //link to large image
No comments:
Post a Comment