Skip to content

Instantly share code, notes, and snippets.

@oritromax
Last active October 10, 2015 06:07
Show Gist options
  • Save oritromax/3645341 to your computer and use it in GitHub Desktop.
Save oritromax/3645341 to your computer and use it in GitHub Desktop.
A Simple Image Resizer Function Using PHP
<?php
//Simple Image Resizing Script Using PHP- Oritro Ahmed
// [www.oritro.com]
// > Require GD library 2.x
// $image is the image file you want to resize
// $type is the type of image file, this function support image/jpeg, image/gif, image/png
// $dest is the Destination of the Generated image
function generate_stview($image,$type,$dest){
list($width, $height) = getimagesize($image);
if($height<"600"){
$newh=$height;
}else {
$newh="600";
}
// Define Your Desired Width
if($width<"800"){
$neww=$width;
}else{
$neww="800";
}
// Define Your Desired Width
if($type=='image/jpeg'){
// Resample
$image_p = imagecreatetruecolor($neww, $newh);
$image_n = imagecreatefromjpeg($image);
imagecopyresampled($image_p, $image_n, 0, 0, 0, 0, $neww, $newh, $width, $height);
$resizedimage=$dest;
imagejpeg($image_p, $resizedimage, 100); // the 100 is the quality of Generated image. Set as You want
$pull=$resizedimage;
}
if($type=='image/png'){
// Resample
$image_p = imagecreatetruecolor($neww, $newh);
$image_n = imagecreatefrompng($image);
imagecopyresampled($image_p, $image_n, 0, 0, 0, 0, $neww, $newh, $width, $height);
$resizedimage=$dest;
imagepng($image_p, $resizedimage);
$pull=$resizedimage;
}
if($type=='image/gif'){
// Resample
$image_p = imagecreatetruecolor($neww, $newh);
$image_n = imagecreatefromgif($image);
imagecopyresampled($image_p, $image_n, 0, 0, 0, 0, $neww, $newh, $width, $height);
$resizedimage=$dest;
imagegif($image_p, $resizedimage, 100);
$pull=$resizedimage;
}
return $pull;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment