TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Cropping In PHP (http://www.talkphp.com/advanced-php-programming/4996-cropping-php.html)

TheCheshiereCat 10-05-2009 08:42 AM

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->= ($width-$this->cropWidth)/2;
   
$this->= ($height-$this->cropHeight)/2;
             
}  

function 
createThumb()
{
                    
  
$thumbSize 250// will create a 250 x 250 thumb
  
$this->thumb imagecreatetruecolor($thumbSize$thumbSize); 

  
imagecopyresampled($this->thumb$this->myImage00,$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

adamdecaf 10-05-2009 09:16 PM

Is it just me or do the functions not appear to be inside the class?

TheCheshiereCat 10-11-2009 02:17 PM

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!

Tanax 10-11-2009 03:30 PM

PHP Code:

$this->myImage 

You forgot an $ before this.

TheCheshiereCat 10-11-2009 04:51 PM

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

Enfernikus 10-11-2009 06:54 PM

The createThumb method would be more flexible if you made ThumbSize an argument defaulted to 250

TheCheshiereCat 10-11-2009 07:16 PM

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.

Tanax 10-12-2009 04:35 AM

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.

TheCheshiereCat 10-12-2009 07:21 AM

Its being cropped though so it won't look distorted either way.

mm88 11-20-2009 10:48 AM

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!

maeltar 11-20-2009 01:26 PM

Quote:

Originally Posted by mm88 (Post 29250)
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);
                }


mm88 11-20-2009 02:06 PM

Hi maeltar, thanks for your reply, but unfortunately your code creates a totally black 400px X 200px rectangle. The image disappears.

Why?

Tanax 11-20-2009 03:16 PM

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);
        }

maeltar 11-20-2009 04:06 PM

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..

mm88 11-20-2009 10:13 PM

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


All times are GMT. The time now is 05:14 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0