user2336648
user2336648

Reputation:

how to crop image and show in different formats using PHP

I want a code when image is uploaded to crop it to 100px * 100px in 3 formats gif,png,jpeg.

I want image to be shown on the page only. No storage needed.

Upvotes: 0

Views: 94

Answers (2)

user2381336
user2381336

Reputation:

<?php

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

$src_x = '0';   
$src_y = '0';   
$src_w = '100';
$src_h = '100'; 
$dst_x = '0';  
$dst_y = '0';  

$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/png");
imagepng($dst_im);
imagedestroy($dst_im);

?>

Upvotes: 3

PHP Code

<?php

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

$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/png");
imagepng($dst_im);
imagegif($dst_im);
imagejpeg($dst_im);

imagedestroy($dst_im);

?>

Upvotes: 0

Related Questions