Jump to content

PHP Image crop


jamiet757

Recommended Posts

I am trying to set up something on my site that will use PHP to crop an image and display it, so that all the images are the same size, but are not squished or distorted. Here is the code I found:

 

<?php
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 found it here: http://www.talkphp.com/advanced-php-programming/1709-cropping-images-using-php.html

 

The problem I am having is with the line that starts with "var". I get this error when I try to access the file:

Parse error: syntax error, unexpected T_VAR

 

I am not exactly sure how to include the var line, or if it is wrong. Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/189683-php-image-crop/
Share on other sites

Well, I still get an error. I fixed a slight problem with a missing $, but I get this error when I try to access thumbcreate.php?src=image.jpg (and yes, the image.jpg file does exist)

 

Fatal error: Cannot access empty property on line 13

 

Here is what I have:

<?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 = 80; // will create a 80 x 80 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(); 

?>

 

Can anyone help me get this script working? I can figure out a few things with php, but my knowledge is limited.

Link to comment
https://forums.phpfreaks.com/topic/189683-php-image-crop/#findComment-1001652
Share on other sites

Tryh being a bit more specific with the image src.

thumbcreate.php?src="./image.jpg"

or

thumbcreate.php?src="$url/image.jpg"

or

thumbcreate.php?src="http://location.of/image.jpg"

 

cause it seems that the $imgSrc is not giving up the image.

 

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/189683-php-image-crop/#findComment-1001662
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.