TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (3) Thread Tools Search this Thread Display Modes
Old 03-04-2012, 10:16 AM   #21 (permalink)
The Wanderer
 
Join Date: Feb 2012
Location: China
Posts: 7
Thanks: 0
nasack is on a distinguished road
Default

That is great! May be you can develop a website to crop image online like this Resize Image Online
__________________
:-PWeb Design Guangzhou
:-PWeb Development
nasack is offline  
Reply With Quote
Old 03-14-2012, 05:33 PM   #22 (permalink)
The Visitor
 
Join Date: Mar 2012
Posts: 1
Thanks: 0
perastikos1 is on a distinguished road
Default

hello
i have just find this post, can anyone help me how to call the crop function?

thanks a lot
perastikos1 is offline  
Reply With Quote
Old 05-17-2012, 10:22 AM   #23 (permalink)
Ivan Ivković, Croatia
 
ivanivkovich's Avatar
 
Join Date: May 2012
Posts: 7
Thanks: 0
ivanivkovich is on a distinguished road
Default

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.
Send a message via Skype™ to ivanivkovich
ivanivkovich is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1709-cropping-images-using-php.html
Posted By For Type Date
PHP PHP GD Library: Cropping Images whilst Maintaining the Aspect Ratio Tutorial This thread Refback 01-10-2008 03:22 PM
PHP Image Manipulation PHP GD Library: Cropping Images whilst Maintaining the Aspect Ratio Tutorial This thread Refback 01-10-2008 03:05 PM
Cropping Images using PHP - TalkPHP This thread Refback 12-29-2007 10:41 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:19 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design