05-17-2012, 10:22 AM
|
#23 (permalink)
|
|
Ivan Ivković, Croatia
Join Date: May 2012
Posts: 7
Thanks: 0
|
This is a great and pretty much all-you-need script!
This is an addon for gif/png/jpg file support since this works for jpeg only.
$file_type = end(explode('.', $this -> imgSrc));
switch($file_type){
case 'jpg':
$file_type = 'jpeg';
break;
case 'gif':
$file_type = 'gif';
break;
case 'jpg':
$file_type = 'png';
break;
}
$imagecreatefromfile_type = 'imagecreatefrom' . $file_type;
$this -> myImage = $imagecreatefromfile_type($this -> imgSrc) or die('Error: Cannot find image!');
Add it to the setImage method.
For perastikos1, the whole script looks like this.
class ImageCropper{
private $imgSrc;
private $myImage;
private $cropHeight;
private $cropWidth;
private $x;
private $y;
private $thumb;
# Sets the image. Thumb function.
public function setImage($image){
$this -> imgSrc = $image;
list($width, $height) = getimagesize($this -> imgSrc);
$file_type = end(explode('.', $this -> imgSrc));
switch($file_type){
case 'jpg':
$file_type = 'jpeg';
break;
case 'gif':
$file_type = 'gif';
break;
case 'jpg':
$file_type = 'png';
break;
}
$imagecreatefromfile_type = 'imagecreatefrom' . $file_type;
$this -> myImage = $imagecreatefromfile_type($this -> imgSrc) or die('Error: Cannot find image!');
$biggestSide = $width > $height ? $width : $height; # Find biggest length
# The crop size will be half that of the largest side
$cropPercent = .5; # Zoom.
$this -> cropWidth = $biggestSide * $cropPercent;
$this -> cropHeight = $biggestSide * $cropPercent;
$this -> x = ($width-$this->cropWidth) / 2;
$this -> y = ($height-$this->cropHeight) / 2;
}
# Creates the image. Thumb function.
public function createThumb(){
$thumbSize = 200;
$thumbSize2 = 150;
$this -> thumb = imagecreatetruecolor($thumbSize, $thumbSize2);
imagecopyresampled($this -> thumb, $this -> myImage, 0, 0,$this -> x, $this -> y, $thumbSize, $thumbSize, $this -> cropWidth, $this -> cropHeight);
}
# Loads the image. Thumb function.
public function renderImage(){
header('Content-type: image/jpeg');
imagejpeg($this -> thumb);
imagedestroy($this -> thumb);
}
}
$image = new ImageCropper();
$image -> setImage($_GET['pic']);
$image -> createThumb();
$image -> renderImage();
Thanks rendair!
Last edited by ivanivkovich : 05-17-2012 at 10:59 AM.
|
|
|