Recently I came across a problem where I had to upload different size of thumbnails to amazon s3, after going through several stackoverflow answers created small function which exactly addressed my requirement.
private function createThumbs( $file, $w, $h){
try{
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// Begin capturing the byte stream
ob_start();
// generate the byte stream
imagejpeg($dst, null, 100);
// and finally retrieve the byte stream
$rawImageBytes = ob_get_clean();
imagedestroy($dst);
return $rawImageBytes;
} catch (\Exception $e){
throw new \Exception($e->getMessage());
}
}
get the image data
// 600px 600px image $image_600_data = $this->createThumbs($_FILES["fileToUpload"]["tmp_name"], 600, 600); // 300px 300 px image $image_300_data = $this->createThumbs($_FILES["fileToUpload"]["tmp_name"], 300, 300); // 100px 100px image $image_100_data = $this->createThumbs($_FILES["fileToUpload"]["tmp_name"], 100, 100);
this will display the 100/300/600 images separately. just to make sure ;)
echo "<img src="data:image/jpeg;base64," . base64_encode( $image_100_path ) . " />";
echo "<img src="data:image/jpeg;base64," . base64_encode( $image_300_path ) . " />";
echo "<img src="data:image/jpeg;base64," . base64_encode( $image_600_path ) . " />";
No comments:
Post a Comment