Last updated on September 6th, 2014 at 09:53 am

On the internet when we upload images then it occupies more size and takes more time load the images due to their heavy size, which result in slow processing of the loading content. So here to solve this problem we can resize the image according to our desired size with PHP. In PHP we can resize any image so that it saves memory space or load in less time. we can use this code to make a full size image according to user dimensions. Some times Users uploads the image as it is which results in  taking much time to load the page and effects the page and internet speed. So to remove these drawbacks user can use this code to manipulate the image to specific dimensions which results in efficient work of web pages.

<?php 
$src = "Penguins.jpg";                                // Accessing Original Image
$extension = strtolower(substr(strrchr($src, '.'), 1));    // getting extension of image
switch($extension)
{
 case 'jpg':
 case 'jpeg':
  $type = 'imagejpeg';
  $img = @imagecreatefromjpeg($src);                   // for jpeg image
 break;
 case 'gif':
  $type = 'imagegif';
  @imagecolortransparent($src, @imagecolorallocate($src, 0, 0, 0));
  $img = @imagecreatefromgif($src);
 break;
 case 'png':
  $type = 'imagepng';
  $img = @imagecreatefrompng($src);
 break;
 default:
  $img = false;
 break;
}
$imageWidth = imagesx($img);
$imageHeight = imagesy($img);
$newWidth = 200;
$newHeight = 200;
$newImg = imagecreatetruecolor($newWidth , $newHeight);
@imagecolortransparent($newImg, @imagecolorallocate($newImg,0,0,0));
@imagealphablending($newImg, true);
imagecopyresampled($newImg, $img , 0, 0, 0, 0, $newWidth, $newHeight , $imageWidth, $imageHeight);
@$type($newImg, 'dev2.png', 90);
@imagedestroy($newImg);
?> 

 

Posted in: php