Home
Website PHP Tutorial
Template Website
Business Info
Blogger Templates
Free CSS Template
Search
Do You Want to use our services or want to establish cooperation please contact us on whatsapp +6285244351343

Artikel Terbaru , 2020-02-16

Tutorial How to crop image with PHP

PenulisIndra Rani

Tutorial How to crop image with PHP

Tutorial How to crop image with PHP

Published : 2020-02-16 by Indra Rani | Website PHP Tutorial

Tutorial How to crop image with PHP

Sitemap Tutorial How to crop image with PHP

Hi all. This time I will share the source code ( Crop Image With PHP ) for those of you who want to make your website lighter and faster than before. One way to improve a website is to crop the image before displaying it.  But before I give the code. I need to tell you that this crop system will automatically convert your image extension to ".WEBP".

This ".WEBP" extension is highly recommended because the processing and image capture is very fast and very light on your hosting memory.

This is a piece of code that you can install in your PHP code.  

Code Crop Image With PHP 

<? php  
$link = "image/";  // FOLDER  IMAGE
$i = str_replace('.webp','',$_GET['img']);  // GET FOR IMG
$url=$link.$i; 
$tmp_names =$url;  

if(!file_exists($url)){

$url = $link."error.png"; // URL IF Image ERROR 

$tmp_names = $url;


$extensionList = array("bmp", "jpg", "gif", "png", "jpeg","webp"); 
$pecah = explode(".", $url); 
foreach($pecah as $new) { 
$new = trim($new); 
$ekstensi = strtolower($new); 

$h = $_GET['height'];  // GET FOR HEIGHT 
$height = $h; 

$w = $_GET['width'];  // GET FOR WIDTH 
$width =$w; 
$q = 80; 
if($w <= 400){ 
$q = 70; 
}  

if (in_array($ekstensi, $extensionList)) 

list($w, $h) = getimagesize($url); 
 /* calculate new image size with ratio */ 
 $ratio = max($width/$w, $height/$h); 
 $h = ceil($height / $ratio); 
 $x = ($w - $width / $ratio) / 2; 
 $w = ceil($width / $ratio); 
 /* new file name */ 
 /* read binary data from image file */ 
 $imgString = $url; 
 /* create image from string */ 

 switch ($ekstensi) { 
   case 'jpeg': 
$image = imagecreatefromjpeg($imgString); 
break; 
   case 'jpg': 
$image = imagecreatefromjpeg($imgString); 
break; 
   case 'png': 
$image = imagecreatefrompng($imgString); 
break; 
   case 'gif': 
$image = imagecreatefromgif($imgString); 
break; 
default: 
     exit; 
     break; 
 } 

$tmp = imagecreatetruecolor($width, $height); 
imagealphablending($tmp, false); 
imagesavealpha($tmp, true); 

 imagecopyresampled($tmp, $image, 
   0, 0, 
   $x, 0, 
   $width, $height, 
   $w, $h); 

header('Content-Type: image/webp'); 
 switch ($ekstensi) { 
   case 'jpeg': 
imagewebp($tmp, null,$q); 
     break; 
   case 'jpg':   
imagewebp($tmp, null,$q); 
     break; 
   case 'png': 
imagewebp($tmp, null,$q); 
     break; 
   case 'gif': 
imagewebp($tmp, null,$q); 

     break; 
   default: 
     exit; 
     break; 
 } 
}   
?>

To use this crop code, all you have to do is call your PHP file and include the (HEIGHT,WIDTH,IMG)  variables. See the calling example below
 

<img src="https://example.com/crop.php?img=image.jpg&height=300&width=200" /> 

 

Crop Image With PHP and HTACCESS

Maybe the URL call above is very long so you need to add the code to your ".htaccess" as below

RewriteRule ^([a-z-]+)/images/?$ crop.php&lang=$1 [NC,L] 
RewriteRule ^([a-z-]+)/images/([0-9-]+)/?$ crop.php?width=$2 [NC,L] 
RewriteRule ^([a-z-]+)/images/([0-9-]+)/([0-9-]+)/?$ crop.php?width=$2&height=$3 [NC,L] 
RewriteRule ^([a-z-]+)/images/([0-9-]+)/([0-9-]+)/(.*)/?$ crop.php?width=$2&height=$3&img=$4 [NC,L]

At the time of calling the image URL. You can only use the code as below

<img src="https://example.com/imgages/400/200/image.jpg"  /> 

That's all the discussion from me about Crop Image With PHP. good luck

6 Latest articles

Update your latest information by increasing reading. Knowledge is not everything but everything requires knowledge

Website PHP Tutorial | 2020-03-09 By Indra Rani

Convert website to a Webview APK

Website PHP Tutorial | 2020-03-09 By Indra Rani

How to make a Keystore to build an APK

Template Website | 2020-03-06 By Indra Rani

Download free admin template website bootstrap4 | Azzara

Template Website | 2020-03-06 By Indra Rani

Download free admin template website | Atlantis

Free CSS Template | 2020-03-03 By Indra Rani

Free css templates button for bootstrap

Template Website | 2020-02-29 By Indra Rani

Download Free template website Bootstrap4