|
@@ -184,10 +184,6 @@ function createThumbnail($source, $max_width, $max_height)
|
|
|
*/
|
|
|
function reencodeImage($fileName, $preferred_format = 0)
|
|
|
{
|
|
|
- // There is nothing we can do without GD, sorry!
|
|
|
- if (!checkGD())
|
|
|
- return false;
|
|
|
-
|
|
|
if (!resizeImageFile($fileName, $fileName . '.tmp', null, null, $preferred_format))
|
|
|
{
|
|
|
if (file_exists($fileName . '.tmp'))
|
|
@@ -201,8 +197,6 @@ function reencodeImage($fileName, $preferred_format = 0)
|
|
|
|
|
|
if (!rename($fileName . '.tmp', $fileName))
|
|
|
return false;
|
|
|
-
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -270,6 +264,23 @@ function checkGD()
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Checks whether the Imagick class is present.
|
|
|
+ *
|
|
|
+ * @return whether or not Imagick is available.
|
|
|
+ */
|
|
|
+function checkIM()
|
|
|
+{
|
|
|
+ static $IM;
|
|
|
+
|
|
|
+ if (isset($IM))
|
|
|
+ return $IM;
|
|
|
+
|
|
|
+ $IM = class_exists('Imagick');
|
|
|
+
|
|
|
+ return $IM;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* See if we have enough memory to thumbnail an image
|
|
|
*
|
|
@@ -312,8 +323,8 @@ function resizeImageFile($source, $destination, $max_width, $max_height, $prefer
|
|
|
{
|
|
|
global $sourcedir;
|
|
|
|
|
|
- // Nothing to do without GD
|
|
|
- if (!checkGD())
|
|
|
+ // Nothing to do without GD or IM
|
|
|
+ if (!checkGD() || !checkIM())
|
|
|
return false;
|
|
|
|
|
|
static $default_formats = array(
|
|
@@ -326,8 +337,6 @@ function resizeImageFile($source, $destination, $max_width, $max_height, $prefer
|
|
|
|
|
|
require_once($sourcedir . '/Subs-Package.php');
|
|
|
|
|
|
- $success = false;
|
|
|
-
|
|
|
// Get the image file, we have to work with something after all
|
|
|
$fp_destination = fopen($destination, 'wb');
|
|
|
if ($fp_destination && substr($source, 0, 7) == 'http://')
|
|
@@ -359,22 +368,25 @@ function resizeImageFile($source, $destination, $max_width, $max_height, $prefer
|
|
|
$sizes = array(-1, -1, -1);
|
|
|
|
|
|
// See if we have -or- can get the needed memory for this operation
|
|
|
- if (!imageMemoryCheck($sizes))
|
|
|
+ if (checkGD() && !imageMemoryCheck($sizes))
|
|
|
return false;
|
|
|
|
|
|
// A known and supported format?
|
|
|
// @todo test PSD and gif.
|
|
|
- if (isset($default_formats[$sizes[2]]) && function_exists('imagecreatefrom' . $default_formats[$sizes[2]]))
|
|
|
+ if (checkIM() && isset($default_formats[$sizes[2]]))
|
|
|
+ {
|
|
|
+ return resizeImage(null, $destination, null, null, $max_width, $max_height, true, $preferred_format);
|
|
|
+ }
|
|
|
+ elseif (checkGD() && isset($default_formats[$sizes[2]]) && function_exists('imagecreatefrom' . $default_formats[$sizes[2]]))
|
|
|
{
|
|
|
$imagecreatefrom = 'imagecreatefrom' . $default_formats[$sizes[2]];
|
|
|
if ($src_img = @$imagecreatefrom($destination))
|
|
|
{
|
|
|
- resizeImage($src_img, $destination, imagesx($src_img), imagesy($src_img), $max_width === null ? imagesx($src_img) : $max_width, $max_height === null ? imagesy($src_img) : $max_height, true, $preferred_format);
|
|
|
- $success = true;
|
|
|
+ return resizeImage($src_img, $destination, imagesx($src_img), imagesy($src_img), $max_width === null ? imagesx($src_img) : $max_width, $max_height === null ? imagesy($src_img) : $max_height, true, $preferred_format);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return $success;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -398,71 +410,95 @@ function resizeImage($src_img, $destName, $src_width, $src_height, $max_width, $
|
|
|
{
|
|
|
global $gd2, $modSettings;
|
|
|
|
|
|
- // Without GD, no image resizing at all.
|
|
|
- if (!checkGD())
|
|
|
- return false;
|
|
|
+ if (checkIM())
|
|
|
+ {
|
|
|
+ static $default_formats = array(
|
|
|
+ '1' => 'gif',
|
|
|
+ '2' => 'jpeg',
|
|
|
+ '3' => 'png',
|
|
|
+ '6' => 'bmp',
|
|
|
+ '15' => 'wbmp'
|
|
|
+ );
|
|
|
+ $preferred_format = empty($preferred_format) || !isset($default_formats[$preferred_format]) ? 2 : $preferred_format;
|
|
|
|
|
|
- $success = false;
|
|
|
+ $imagick = New Imagick($destName);
|
|
|
+ $src_width = empty($src_width) ? $imagick->getImageWidth() : $src_width;
|
|
|
+ $src_height = empty($src_height) ? $imagick->getImageHeight() : $src_height;
|
|
|
+ $dest_width = empty($max_width) ? $src_width : $max_width;
|
|
|
+ $dest_height = empty($max_height) ? $src_height : $max_height;
|
|
|
|
|
|
- // Determine whether to resize to max width or to max height (depending on the limits.)
|
|
|
- if (!empty($max_width) || !empty($max_height))
|
|
|
+ $imagick->setImageFormat($default_formats[$preferred_format]);
|
|
|
+ $success = $imagick->writeImage($destName);
|
|
|
+
|
|
|
+ return !empty($success);
|
|
|
+ }
|
|
|
+ elseif (checkGD())
|
|
|
{
|
|
|
- if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height))
|
|
|
- {
|
|
|
- $dst_width = $max_width;
|
|
|
- $dst_height = floor($src_height * $max_width / $src_width);
|
|
|
- }
|
|
|
- elseif (!empty($max_height))
|
|
|
- {
|
|
|
- $dst_width = floor($src_width * $max_height / $src_height);
|
|
|
- $dst_height = $max_height;
|
|
|
- }
|
|
|
+ $success = false;
|
|
|
|
|
|
- // Don't bother resizing if it's already smaller...
|
|
|
- if (!empty($dst_width) && !empty($dst_height) && ($dst_width < $src_width || $dst_height < $src_height || $force_resize))
|
|
|
+ // Determine whether to resize to max width or to max height (depending on the limits.)
|
|
|
+ if (!empty($max_width) || !empty($max_height))
|
|
|
{
|
|
|
- // (make a true color image, because it just looks better for resizing.)
|
|
|
- if ($gd2)
|
|
|
+ if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height))
|
|
|
+ {
|
|
|
+ $dst_width = $max_width;
|
|
|
+ $dst_height = floor($src_height * $max_width / $src_width);
|
|
|
+ }
|
|
|
+ elseif (!empty($max_height))
|
|
|
{
|
|
|
- $dst_img = imagecreatetruecolor($dst_width, $dst_height);
|
|
|
+ $dst_width = floor($src_width * $max_height / $src_height);
|
|
|
+ $dst_height = $max_height;
|
|
|
+ }
|
|
|
|
|
|
- // Deal nicely with a PNG - because we can.
|
|
|
- if ((!empty($preferred_format)) && ($preferred_format == 3))
|
|
|
+ // Don't bother resizing if it's already smaller...
|
|
|
+ if (!empty($dst_width) && !empty($dst_height) && ($dst_width < $src_width || $dst_height < $src_height || $force_resize))
|
|
|
+ {
|
|
|
+ // (make a true color image, because it just looks better for resizing.)
|
|
|
+ if ($gd2)
|
|
|
{
|
|
|
- imagealphablending($dst_img, false);
|
|
|
- if (function_exists('imagesavealpha'))
|
|
|
- imagesavealpha($dst_img, true);
|
|
|
+ $dst_img = imagecreatetruecolor($dst_width, $dst_height);
|
|
|
+
|
|
|
+ // Deal nicely with a PNG - because we can.
|
|
|
+ if ((!empty($preferred_format)) && ($preferred_format == 3))
|
|
|
+ {
|
|
|
+ imagealphablending($dst_img, false);
|
|
|
+ if (function_exists('imagesavealpha'))
|
|
|
+ imagesavealpha($dst_img, true);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- else
|
|
|
- $dst_img = imagecreate($dst_width, $dst_height);
|
|
|
+ else
|
|
|
+ $dst_img = imagecreate($dst_width, $dst_height);
|
|
|
|
|
|
- // Resize it!
|
|
|
- if ($gd2)
|
|
|
- imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
|
|
|
+ // Resize it!
|
|
|
+ if ($gd2)
|
|
|
+ imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
|
|
|
+ else
|
|
|
+ imagecopyresamplebicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
|
|
|
+ }
|
|
|
else
|
|
|
- imagecopyresamplebicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
|
|
|
+ $dst_img = $src_img;
|
|
|
}
|
|
|
else
|
|
|
$dst_img = $src_img;
|
|
|
+
|
|
|
+ // Save the image as ...
|
|
|
+ if (!empty($preferred_format) && ($preferred_format == 3) && function_exists('imagepng'))
|
|
|
+ $success = imagepng($dst_img, $destName);
|
|
|
+ elseif (!empty($preferred_format) && ($preferred_format == 1) && function_exists('imagegif'))
|
|
|
+ $success = imagegif($dst_img, $destName);
|
|
|
+ elseif (function_exists('imagejpeg'))
|
|
|
+ $success = imagejpeg($dst_img, $destName);
|
|
|
+
|
|
|
+ // Free the memory.
|
|
|
+ imagedestroy($src_img);
|
|
|
+ if ($dst_img != $src_img)
|
|
|
+ imagedestroy($dst_img);
|
|
|
+
|
|
|
+ return $success;
|
|
|
}
|
|
|
else
|
|
|
- $dst_img = $src_img;
|
|
|
-
|
|
|
- // Save the image as ...
|
|
|
- if (!empty($preferred_format) && ($preferred_format == 3) && function_exists('imagepng'))
|
|
|
- $success = imagepng($dst_img, $destName);
|
|
|
- elseif (!empty($preferred_format) && ($preferred_format == 1) && function_exists('imagegif'))
|
|
|
- $success = imagegif($dst_img, $destName);
|
|
|
- elseif (function_exists('imagejpeg'))
|
|
|
- $success = imagejpeg($dst_img, $destName);
|
|
|
-
|
|
|
- // Free the memory.
|
|
|
- imagedestroy($src_img);
|
|
|
- if ($dst_img != $src_img)
|
|
|
- imagedestroy($dst_img);
|
|
|
-
|
|
|
- return $success;
|
|
|
+ // Without GD, no image resizing at all.
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -735,7 +771,7 @@ function gif_outputAsPng($gif, $lpszFileName, $background_color = -1)
|
|
|
*/
|
|
|
function showCodeImage($code)
|
|
|
{
|
|
|
- global $settings, $user_info, $modSettings;
|
|
|
+ global $gd2, $settings, $user_info, $modSettings;
|
|
|
|
|
|
// Note: The higher the value of visual_verification_type the harder the verification is - from 0 as disabled through to 4 as "Very hard".
|
|
|
|
|
@@ -773,11 +809,6 @@ function showCodeImage($code)
|
|
|
// Give the image a border?
|
|
|
$hasBorder = $simpleBGColor;
|
|
|
|
|
|
- // Is this GD2? Needed for pixel size.
|
|
|
- $testGD = get_extension_funcs('gd');
|
|
|
- $gd2 = in_array('imagecreatetruecolor', $testGD) && function_exists('imagecreatetruecolor');
|
|
|
- unset($testGD);
|
|
|
-
|
|
|
// The amount of pixels inbetween characters.
|
|
|
$character_spacing = 1;
|
|
|
|