SimpleImage::crop PHP Method

crop() public method

Crop an image
public crop ( integer $x1, integer $y1, integer $x2, integer $y2 ) : SimpleImage
$x1 integer Left
$y1 integer Top
$x2 integer Right
$y2 integer Bottom
return SimpleImage
    function crop($x1, $y1, $x2, $y2)
    {
        // Determine crop size
        if ($x2 < $x1) {
            list($x1, $x2) = array($x2, $x1);
        }
        if ($y2 < $y1) {
            list($y1, $y2) = array($y2, $y1);
        }
        $crop_width = $x2 - $x1;
        $crop_height = $y2 - $y1;
        // Perform crop
        $new = imagecreatetruecolor($crop_width, $crop_height);
        imagealphablending($new, false);
        imagesavealpha($new, true);
        imagecopyresampled($new, $this->image, 0, 0, $x1, $y1, $crop_width, $crop_height, $crop_width, $crop_height);
        // Update meta data
        $this->width = $crop_width;
        $this->height = $crop_height;
        $this->image = $new;
        return $this;
    }

Usage Example

Beispiel #1
0
		function cropImage($imagePath, $width, $height) {
			global $module;
			
			$folderPath = $this->crop_folder;
			
			$parent_path = str_replace("/images/crop","",$folderPath);
			$parent_path = str_replace("\\images\\crop","",$parent_path);
			
			if(!JFolder::exists($parent_path)){
				if(mkdir($parent_path, 0777)){
				
				} else {
					echo "Error: Can't create folder for resize image!"; exit();
				}
			}
			
			$parent_path = str_replace("/crop","",$folderPath);
			$parent_path = str_replace("\\crop","",$parent_path);
			 if(!JFolder::exists($parent_path)){
					//if(JFolder::create($folderPath)){echo "ok";} else {echo "not ok";}	 
					if(mkdir($parent_path, 0777)){
						if(mkdir($folderPath, 0777)){
						
						} else {
							echo "Error: Can't create folder for crop image!"; 
						}	
					} else {
						echo "Error: Can't create folder for crop image!"; 
					}	 
			 } else
			 {
				 if(!JFolder::exists($folderPath)){
					if(mkdir($folderPath, 0777)){
						
					} else {
						echo "Error: Can't create folder for crop image!"; 
					}	
				 
				 }
			 }
			 
			$nameImg = str_replace('/','',strrchr($imagePath,"/")); 
			$ext = substr($nameImg, strrpos($nameImg, '.'));
			$file_name = substr($nameImg, 0,  strrpos($nameImg, '.'));
			$nameImg = str_replace(" ","",$file_name . "_" . $width . "_" . $height .  $ext);	 
			 
			 if(!JFile::exists($folderPath.DS.$nameImg)){
				 $image = new SimpleImage();
				 $image->load($imagePath);
				 $image->crop($width,$height);
				 $image->save($folderPath.DS.$nameImg);
			 }else{
					 list($info_width, $info_height) = @getimagesize($folderPath.DS.$nameImg);
					 if($width!=$info_width||$height!=$info_height){
						 $image = new SimpleImage();
						 $image->load($imagePath);
						 $image->crop($width,$height);
						 $image->save($folderPath.DS.$nameImg);
					 }
			 }
			 
			 return $this->url_to_crop . $nameImg;
		}
All Usage Examples Of SimpleImage::crop