jamiet757 Posted January 25, 2010 Share Posted January 25, 2010 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 More sharing options...
teamatomic Posted January 25, 2010 Share Posted January 25, 2010 You should go back and read the tutorial again. You missed the most important part. class cropImage{} HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/189683-php-image-crop/#findComment-1001092 Share on other sites More sharing options...
jamiet757 Posted January 25, 2010 Author Share Posted January 25, 2010 I have that surrounding the area where the image appears, is that not the correct way to do it? Link to comment https://forums.phpfreaks.com/topic/189683-php-image-crop/#findComment-1001094 Share on other sites More sharing options...
jamiet757 Posted January 25, 2010 Author Share Posted January 25, 2010 i put the class tag around it, hopefully it will work Link to comment https://forums.phpfreaks.com/topic/189683-php-image-crop/#findComment-1001095 Share on other sites More sharing options...
jamiet757 Posted January 26, 2010 Author Share Posted January 26, 2010 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 More sharing options...
teamatomic Posted January 26, 2010 Share Posted January 26, 2010 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 More sharing options...
jamiet757 Posted January 26, 2010 Author Share Posted January 26, 2010 I tried your suggestions, but to no avail I think the problem lies in this line (obviously since it says so in the error message) $this->imgSrc = $image; I don't understand where it is getting the $image from, it is not referenced anywhere else. Link to comment https://forums.phpfreaks.com/topic/189683-php-image-crop/#findComment-1001664 Share on other sites More sharing options...
jamiet757 Posted January 26, 2010 Author Share Posted January 26, 2010 Can anyone help me out with this one? Link to comment https://forums.phpfreaks.com/topic/189683-php-image-crop/#findComment-1002075 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.