Skip to content

Instantly share code, notes, and snippets.

@solancer
Forked from slivero/gist:3164297
Created December 16, 2015 00:37
Show Gist options
  • Save solancer/6e0bab55e97cfb6d0d5b to your computer and use it in GitHub Desktop.
Save solancer/6e0bab55e97cfb6d0d5b to your computer and use it in GitHub Desktop.
Resampling a JPEG image in PHP using GD (not imagemagick)
<?php
/**
* Class used for resampling images
*/
class Resampler
{
/**
* Resample an image
*
* The image is returned as a string ready to be saved, it is not converteed back to a resource
* as that would just be unnecessary
*
* @param resource $image Resource storing JPEG image
* @param integer $dpi The dpi the image should be resampled at
* @return string Resampled JPEG image as a string
*/
function resample($image, $height, $width, $dpi = 300)
{
if(!$image)
{
throw new \Exception('Attempting to resample an empty image');
}
if(gettype($image) !== 'resource')
{
throw new \Exception('Attempting to resample something which is not a resource');
}
//Use truecolour image to avoid any issues with colours changing
$tmp_img = imagecreatetruecolor($width, $height);
//Resample the image to be ready for print
if(!imagecopyresampled ($tmp_img , $image , 0 , 0 ,0 , 0 , $width , $height , imagesx($image) , imagesy($image)))
{
throw new \Exception("Unable to resample image");
}
//Massive hack to get the image as a jpeg string but there is no php function which converts
//a GD image resource to a JPEG string
ob_start();
imagejpeg($tmp_img, "", 100);
$image = ob_get_contents();
ob_end_clean();
//change the JPEG header to 300 pixels per inch
$image = substr_replace($image, pack("Cnn", 0x01, 300, 300), 13, 5);
return $image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment