 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
10-05-2009, 08:42 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Oct 2009
Posts: 5
Thanks: 0
|
Cropping In PHP
Hello everyone
I stumbled upon <a href=http://www.talkphp.com/advanced-php-programming/1709-cropping-images-using-php.html>this great tutorial</a> on how to crop images using PHP.
I created a page called thumbCreate.php containing the following code from the tutorial:
PHP Code:
<?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 = 250; // will create a 250 x 250 thumb $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); }
$image = new cropImage; $image->setImage($src); $image->createThumb(); $image->renderImage();
?>
I then wrote this in another php page:
PHP Code:
<img src='createThumb.php?src=gallery/1.jpg' border='0' alt=".$picture[0].">
However, the image will not show, even though I definitely do have an image stored at gallery/1.jpg.
Although I'm familiar with OOP programming in Java, this is my first time trying it with PHP, so perhaps my syntax is wrong.
Any help would be greatly appreciated.
THANKS in advance!
Tom
|
|
|
|
|
The Following User Says Thank You to TheCheshiereCat For This Useful Post:
|
|
10-05-2009, 09:16 PM
|
#2 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
Is it just me or do the functions not appear to be inside the class?
|
|
|
|
10-11-2009, 02:17 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Oct 2009
Posts: 5
Thanks: 0
|
You're right, sorry.
I think the code might actually be wrong though, because it says theres a parse error on line 11, which is:
Code:
this->myImage = imagecreatefromjpeg($image$this->imgSrc) or die("Error: Cannot find image!");
Could you please help me further? This is the very last thing I need to complete a website for a client.
Thanks!
|
|
|
|
10-11-2009, 03:30 PM
|
#4 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
You forgot an $ before this.
__________________
|
|
|
|
10-11-2009, 04:51 PM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Oct 2009
Posts: 5
Thanks: 0
|
Thanks everyone, it worked!
Heres the working code for reference:
Code:
<?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 = 250; // will create a 250 x 250 thumb
$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();
?>
thanks again
|
|
|
|
10-11-2009, 06:54 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
The createThumb method would be more flexible if you made ThumbSize an argument defaulted to 250
|
|
|
|
10-11-2009, 07:16 PM
|
#7 (permalink)
|
|
The Wanderer
Join Date: Oct 2009
Posts: 5
Thanks: 0
|
Thats what I did for my own project; I just wanted to get the above to work, and when it did I changed it so that the thumbs have a fixed height/width of 150.
|
|
|
|
10-12-2009, 04:35 AM
|
#8 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
What if they upload an image that is 600*900(taller than wider) or 900*600(wider than taller)? The thumb would be weird if it got 250*250 since it's not resized according to the original.
__________________
|
|
|
|
10-12-2009, 07:21 AM
|
#9 (permalink)
|
|
The Wanderer
Join Date: Oct 2009
Posts: 5
Thanks: 0
|
Its being cropped though so it won't look distorted either way.
|
|
|
|
11-20-2009, 10:48 AM
|
#10 (permalink)
|
|
The Visitor
Join Date: Nov 2009
Posts: 4
Thanks: 0
|
Hi, Compliments! Great Code!
Only a question:
your script creates a 250 x 250 cropped thumb.
In which way can I modify the script to create a thumb with different width and height?
For example, to create a 400 (w) x 200 (h) cropped thumb ?
thanks!
|
|
|
|
11-20-2009, 01:26 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
|
Quote:
Originally Posted by mm88
Hi, Compliments! Great Code!
Only a question:
your script creates a 250 x 250 cropped thumb.
In which way can I modify the script to create a thumb with different width and height?
For example, to create a 400 (w) x 200 (h) cropped thumb ?
thanks!
|
At a quick glance, change the following..
Code:
function createThumb(){
$thumbSize = 250; // will create a 250 x 250 thumb
$this->thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight);
}
to
Code:
function createThumb(){
$thumbSize1 = 400;
$thumbSize2 = 200;
$this->thumb = imagecreatetruecolor($thumbSize1, $thumbSize2);
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight);
}
__________________
Thanks... Simon
Sex, Drugs & Linux Rules
|
|
|
11-20-2009, 02:06 PM
|
#12 (permalink)
|
|
The Visitor
Join Date: Nov 2009
Posts: 4
Thanks: 0
|
Hi maeltar, thanks for your reply, but unfortunately your code creates a totally black 400px X 200px rectangle. The image disappears.
Why?
|
|
|
|
11-20-2009, 03:16 PM
|
#13 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Change
php Code:
function createThumb(){ $thumbSize = 250; // will create a 250 x 250 thumb $this->thumb = imagecreatetruecolor($thumbSize, $thumbSize); imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight); }
to
php Code:
function createThumb(){ $thumbSize1 = 400; $thumbSize2 = 200; $this->thumb = imagecreatetruecolor($thumbSize1, $thumbSize2); imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize1, $thumbSize2, $this->cropWidth, $this->cropHeight); }
__________________
|
|
|
|
11-20-2009, 04:06 PM
|
#14 (permalink)
|
|
The Acquainted
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
|
because I didn't change..
Code:
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight);
sorry bout that, Tanax spotted my mistake..
__________________
Thanks... Simon
Sex, Drugs & Linux Rules
|
|
|
11-20-2009, 10:13 PM
|
#15 (permalink)
|
|
The Visitor
Join Date: Nov 2009
Posts: 4
Thanks: 0
|
Thanks guys for your replies, but the script now stretches the image.
Here an example:
the original image:
and the cropped result:
 ]
The cropped one is distorted and with 2 black stripes.
Why?
many thanks
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|