<?php
function resizeFile($source, $destination, $newwidth, $newheight, $chmod=0777)
{
$thumb = resize($source, $newwidth, $newheight);
if ($thumb) {
// mkdir(dirname($destination)); // @TODO: ensure destination exists
imagejpeg($thumb, $destination);
chmod($destination, $chmod); // file rights
} else {
return false; // throwing an exception is better that this ...
}
}
function resize($source, $newwidth, $newheight)
{
if (!is_file($source)) {
return false;
}
$image = null;
if (preg_match("/.jpg/i", "$source")) { // mimeType: image/jpeg
$image = imagecreatefromjpeg($source);
} else if (preg_match("/.gif/i", "$source")) { // mimeType: image/gif
$image = imagecreatefromgif($source);
} else if (preg_match("/.png/i", "$source")) { // mimeType: image/png
$image = imagecreatefrompng($source);
} else {
return false; // unknown format
}
list($width, $height) = getimagesize($source);
if($newwidth < $width && $newheight < $height){
$widthdiff = $width -$newwidth;
$heightdiff = $height -$newheight;
if($heightdiff > $widthdiff){
$newwidth = ceil( $width*$newheight/$height);
}else{
$newheight = ceil( $height*$newwidth/$width);
}
}else if($newwidth < $width){
$newheight = ceil( $height*$newwidth/$width);
}else if($newheight < $height){
$newwidth = ceil( $width*$newheight/$height);
} else {
$newwidth = $width;
$newheight = $height;
}
$gd_version = gd_version();
//$gd_version = 3;
$thumb = null;
if ($gd_version >= 2) {
$thumb = imagecreatetruecolor($newwidth, $newheight);
} else {
$thumb = imagecreate($newwidth, $newheight);
}
// imagealphablending($thumb, false);
// Resize
//imagecopyresized($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Resample
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $thumb;
}
function image2jpeg($source, $destination, $type=null)
{
if (!is_file($source)) {
return false;
}
$image = null;
// alternatively & better: get file extension and use it instead of preg_match
if (preg_match("/.jpg$/i", $source) ||preg_match("/.jpeg$/i", $source) ||"jpg"==$type) { // mimeType: image/jpeg
$image = imagecreatefromjpeg($source);
} else if (preg_match("/.gif$/i", $source) || "gif"==$type) { // mimeType: image/gif
$image = imagecreatefromgif($source);
} else if (preg_match("/.png$/i", $source) || "png"==$type) { // mimeType: image/png
$image = imagecreatefrompng($source);
} else {
return false; // unknown format
}
if (!$image) {
return false;
}
mkpath(dirname($destination));
imagejpeg($image, $destination);
return true;
}
function webShowImage($source)
{
Header("Content-type: image/jpeg");
$im = imagecreatefromjpeg("$source");
Imagejpeg($im, '', 100);
ImageDestroy($im);
}
function webResizeImage($source, $newwidth, $newheight)
{
$thumb = resize($source, $newwidth, $newheight);
header('Content-type: image/jpeg');
imagejpeg($thumb);
}
function gd_version() {
static $gd_version_number = null;
if ($gd_version_number === null) {
// Use output buffering to get results from phpinfo()
// without disturbing the page we're in. Output
// buffering is "stackable" so we don't even have to
// worry about previous or encompassing buffering.
ob_start();
phpinfo(INFO_MODULES); // or 8
$module_info = ob_get_contents();
ob_end_clean();
if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i",
$module_info,$matches)) {
$gd_version_number = $matches[1];
} else {
$gd_version_number = 0;
}
}
return $gd_version_number;
}
?>