ad

How to Crop And Upload Image with PHP

image-crop

Many times we need to crop the photo in the web application. This is simple PHP code which will crop 100px X 100 px photo. Change the code according to your need. You can also add some more effects to the code and integrate it with some big applications.

The code is not similar to the snapshot. This code will crop the image specified in $filename variable.

Crop image with PHP

<?php

$filename= "test.jpg";
list($w, $h, $type, $attr) = getimagesize($filename);
$src_im = imagecreatefromjpeg($filename);
$jpeg_quality = 90;

$src_x = '0'; // begin x
$src_y = '0'; // begin y
$src_w = '100'; // width
$src_h = '100'; // height
$dst_x = '0'; // destination x
$dst_y = '0'; // destination y

$dst_im = imagecreatetruecolor($src_w, $src_h);
$white = imagecolorallocate($dst_im, 255, 255, 255);
imagefill($dst_im, 0, 0, $white);

imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);

header("Content-type: image/jpeg");
imagejpeg($dst_im);
imagedestroy($dst_im);

?>

Save Cropped image on server

Above code will show the cropped part of the image in the browser. If you want to save this cropped image. you should replace header code with the other line of code. See below:

//header('Content-type: image/jpg');
imagejpeg($dst_im,PATH_TO_SAVE_IMAGE,$jpeg_quality);

 

Tags: | | |
Nord VPN Sale

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Developed By Deepanker