Crop Image in Core PHP

Crop Image in Core PHP

Today Discussed Crop Image in Core PHP. Image crop and create thumb image and upload save particular directory. crop this image and resize compressed image save a directory. code follow us. 
Crop Image in Core PHP
Crop Image in Core PHP
$upload_dir = "upload_pic";     // The directory for the images to be saved in
$upload_path = $upload_dir."/";    // The path to where the image will be saved
$large_image_prefix = "resize_";    // The prefix name to large image
$thumb_image_prefix = "thumbnail_";   // The prefix name to the thumb image
$large_image_name = $large_image_prefix.$_SESSION['random_key'];     // New name of the large image (append the timestamp to the filename)
$thumb_image_name = $thumb_image_prefix.$_SESSION['random_key'];     // New name of the thumbnail image (append the timestamp to the filename)
$max_file = "3";        // Maximum file size in MB
$max_width = "500";       // Max width allowed for the large image
$thumb_width = "100";      // Width of thumbnail image
$thumb_height = "100";      // Height of thumbnail image
// Only one of these image types should be allowed for upload
$allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
$allowed_image_ext = array_unique($allowed_image_types); // do not change this
$image_ext = ""; // initialise variable, do not change this.
foreach ($allowed_image_ext as $mime_type => $ext) {
    $image_ext.= strtoupper($ext)." ";
}

Image Resize Function


function resizeImage($image,$width,$height,$scale) {
 list($imagewidth, $imageheight, $imageType) = getimagesize($image);
 $imageType = image_type_to_mime_type($imageType);
 $newImageWidth = ceil($width * $scale);
 $newImageHeight = ceil($height * $scale);
 $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
 switch($imageType) {
  case "image/gif":
   $source=imagecreatefromgif($image); 
   break;
     case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
   $source=imagecreatefromjpeg($image); 
   break;
     case "image/png":
  case "image/x-png":
   $source=imagecreatefrompng($image); 
   break;
   }
 imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
 
 switch($imageType) {
  case "image/gif":
     imagegif($newImage,$image); 
   break;
       case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
     imagejpeg($newImage,$image,90); 
   break;
  case "image/png":
  case "image/x-png":
   imagepng($newImage,$image);  
   break;
    }
 
 chmod($image, 0777);
 return $image;
}
Resize Thumbnail Image Function

function resizeImage($image,$width,$height,$scale) {
 list($imagewidth, $imageheight, $imageType) = getimagesize($image);
 $imageType = image_type_to_mime_type($imageType);
 $newImageWidth = ceil($width * $scale);
 $newImageHeight = ceil($height * $scale);
 $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
 switch($imageType) {
  case "image/gif":
   $source=imagecreatefromgif($image); 
   break;
     case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
   $source=imagecreatefromjpeg($image); 
   break;
     case "image/png":
  case "image/x-png":
   $source=imagecreatefrompng($image); 
   break;
   }
 imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
 
 switch($imageType) {
  case "image/gif":
     imagegif($newImage,$image); 
   break;
       case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
     imagejpeg($newImage,$image,90); 
   break;
  case "image/png":
  case "image/x-png":
   imagepng($newImage,$image);  
   break;
    }
 
 chmod($image, 0777);
 return $image;
}
function getHeight($image) {
 $size = getimagesize($image);
 $height = $size[1];
 return $height;
}
//You do not need to alter these functions
function getWidth($image) {
 $size = getimagesize($image);
 $width = $size[0];
 return $width;
}

if (isset($_POST["upload_thumbnail"]) && strlen($large_photo_exists)>0) {
 //Get the new coordinates to crop the image.
 $x1 = $_POST["x1"];
 $y1 = $_POST["y1"];
 $x2 = $_POST["x2"];
 $y2 = $_POST["y2"];
 $w = $_POST["w"];
 $h = $_POST["h"];
 //Scale the image to the thumb_width set above
 $scale = $thumb_width/$w;
 $cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
 //Reload the page again to view the thumbnail
 header("location:".$_SERVER["PHP_SELF"]);
 exit();
}


if ($_GET['a']=="delete" && strlen($_GET['t'])>0){
//get the file locations 
 $large_image_location = $upload_path.$large_image_prefix.$_GET['t'];
 $thumb_image_location = $upload_path.$thumb_image_prefix.$_GET['t'];
 if (file_exists($large_image_location)) {
  unlink($large_image_location);
 }
 if (file_exists($thumb_image_location)) {
  unlink($thumb_image_location);
 }
 header("location:".$_SERVER["PHP_SELF"]);
 exit(); 
}
view.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <meta name="generator" content="WebMotionUK" />
 <title>WebMotionUK - PHP &amp; Jquery image upload &amp; crop</title>
 <script type="text/javascript" src="js/jquery-pack.js"></script>
 <script type="text/javascript" src="js/jquery.imgareaselect.min.js"></script>
</head>
<body>

Crop Image in Core PHP Dev2Tricks 5 of 5
Crop Image in Core PHP Today Discussed Crop Image in Core PHP. Image crop and create thumb image and upload save particular directory. c...

Share this

Related Posts

Previous
Next Post »