1
0

use magick for image resize

This commit is contained in:
Mike Schwörer 2018-02-03 23:49:44 +01:00
parent f44e7e6ace
commit ca07729318
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 40 additions and 19 deletions

View File

@ -187,23 +187,20 @@ function clearLoginCookie()
/** /**
* easy image resize function * easy image resize function
* @author http://www.nimrodstech.com/php-image-resize/ * @author http://www.nimrodstech.com/php-image-resize/
* @param $file - file name to resize * @param string $file - file name to resize
* @param $string - The image data, as a string * @param int $width - new image width
* @param $width - new image width * @param int $height - new image height
* @param $height - new image height * @param boolean $proportional - keep image proportional, default is no
* @param $proportional - keep image proportional, default is no * @param string $output - name of the new file (include path if needed)
* @param $output - name of the new file (include path if needed)
* @param $quality - enter 1-100 (100 is best quality) default is 100
* @return boolean|resource * @return boolean|resource
*/ */
function smart_resize_image($file, $string = null, $width = 0, $height = 0, $proportional = false, $output = 'file', $quality = 100 function smart_resize_image($file, $width = 0, $height = 0, $proportional, $output)
) { {
if ( $height <= 0 && $width <= 0 ) return false; if ( $height <= 0 && $width <= 0 ) return false;
if ( $file === null && $string === null ) return false; if ( $file === null) return false;
# Setting defaults and meta # Setting defaults and meta
$info = $file !== null ? getimagesize($file) : getimagesizefromstring($string); $info = getimagesize($file);
$image = ''; $image = '';
$final_width = 0; $final_width = 0;
$final_height = 0; $final_height = 0;
@ -232,9 +229,9 @@ function smart_resize_image($file, $string = null, $width = 0, $height = 0, $pro
# Loading image to memory according to type # Loading image to memory according to type
switch ( $info[2] ) { switch ( $info[2] ) {
case IMAGETYPE_JPEG: $file !== null ? $image = imagecreatefromjpeg($file) : $image = imagecreatefromstring($string); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($file); break;
case IMAGETYPE_GIF: $file !== null ? $image = imagecreatefromgif($file) : $image = imagecreatefromstring($string); break; case IMAGETYPE_GIF: $image = imagecreatefromgif($file); break;
case IMAGETYPE_PNG: $file !== null ? $image = imagecreatefrompng($file) : $image = imagecreatefromstring($string); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($file); break;
default: return false; default: return false;
} }
@ -280,9 +277,9 @@ function smart_resize_image($file, $string = null, $width = 0, $height = 0, $pro
# Writing image according to type to the output destination and image quality # Writing image according to type to the output destination and image quality
switch ( $info[2] ) { switch ( $info[2] ) {
case IMAGETYPE_GIF: imagegif($image_resized, $output); break; case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, $quality); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, 100); break;
case IMAGETYPE_PNG: case IMAGETYPE_PNG:
$quality = 9 - (int)((0.9*$quality)/10.0); $quality = 9 - (int)((0.9*100)/10.0);
imagepng($image_resized, $output, $quality); imagepng($image_resized, $output, $quality);
break; break;
default: return false; default: return false;
@ -291,6 +288,19 @@ function smart_resize_image($file, $string = null, $width = 0, $height = 0, $pro
return true; return true;
} }
/**
* @param string $file - file name to resize
* @param int $width - new image width
* @param int $height - new image height
* @param string $output - name of the new file (include path if needed)
*/
function magick_resize_image($file, $width, $height, $output)
{
$cmd = 'convert "' . $file . '" -strip -resize ' . $width . 'x' . $height . ' "' . $output . '"';
shell_exec($cmd);
}
function sendMail($subject, $content, $to, $from) { function sendMail($subject, $content, $to, $from) {
mail($to, $subject, $content, 'From: ' . $from); mail($to, $subject, $content, 'From: ' . $from);
} }

View File

@ -74,10 +74,16 @@ class Books
public static function createPreview($prog) public static function createPreview($prog)
{ {
global $CONFIG;
$src = $prog['imgfront_path']; $src = $prog['imgfront_path'];
$dst = $prog['preview_path']; $dst = $prog['preview_path'];
smart_resize_image($src , null, 200, 0, true, $dst, 100); if ($CONFIG['use_magick'])
magick_resize_image($src, 200, 0, $dst);
else
smart_resize_image($src, 200, 0, true, $dst);
} }
public static function getBook($id) public static function getBook($id)

View File

@ -240,9 +240,14 @@ class Programs
public static function createPreview($prog) public static function createPreview($prog)
{ {
global $CONFIG;
$src = $prog['mainimage_path']; $src = $prog['mainimage_path'];
$dst = $prog['preview_path']; $dst = $prog['preview_path'];
smart_resize_image($src , null, 250, 0, true, $dst, 100); if ($CONFIG['use_magick'])
magick_resize_image($src, 250, 0, $dst);
else
smart_resize_image($src, 250, 0, true, $dst);
} }
} }