Esempio n. 1
0
 /**
  * Get an image from a file or url, return it resized if the image exceeds the $maxImageWidth or $maxImageHeight directives.
  *
  * The return value is an array.
  * ['width'] is the width of the image.
  * ['height'] is the height of the image.
  * ['mime'] is the mime type of the image. Resized images are always in jpeg format.
  * ['image'] is the image data.
  * ['ext'] is the extension of the image file.
  *
  * @param string $source path or url to file.
  *
  * @return array|bool
  */
 function getImage($source)
 {
     $width = -1;
     $height = -1;
     $mime = "application/octet-stream";
     $ext = "";
     $image = $this->getFileContents($source);
     $ratio = 1;
     if ($image !== false && strlen($image) > 0) {
         if (BinStringStatic::startsWith(trim($image), '<svg') || (BinStringStatic::startsWith(trim($image), '<?xml') || strpos($image, '<svg') > 0)) {
             // SVG image.
             $xml = simplexml_load_string($image);
             $attr = $xml->attributes();
             $meta = $this->handleSVGAttribs($xml);
             $mime = "image/svg+xml";
             $ext = "svg";
             $width = $meta['width'];
             $height = $meta['height'];
             $ratio = $this->getImageScale($width, $height);
             if ($ratio < 1) {
                 $attr->width = $width * $ratio;
                 $attr->height = $height * $ratio;
             }
             $image = $xml->asXML();
         } else {
             $imageFile = imagecreatefromstring($image);
             if ($imageFile !== false) {
                 $width = ImageSX($imageFile);
                 $height = ImageSY($imageFile);
             }
             if ($this->isExifInstalled) {
                 @($type = exif_imagetype($source));
                 $mime = image_type_to_mime_type($type);
             }
             if ($mime === "application/octet-stream") {
                 $mime = $this->image_file_type_from_binary($image);
             }
             if ($mime === "application/octet-stream") {
                 $mime = $this->getMimeTypeFromUrl($source);
             }
         }
     } else {
         return false;
     }
     if ($width <= 0 || $height <= 0) {
         return false;
     }
     if ($mime !== "image/svg+xml" && $this->isGdInstalled) {
         $ratio = $this->getImageScale($width, $height);
         //echo "<pre>\$source: $source\n\$ratio.: $ratio\n\$mime..: $mime\n\$width.: $width\n\$height: $height</pre>\n";
         if ($ratio < 1 || empty($mime)) {
             if ($mime == "image/png" || $this->isGifImagesEnabled === false && $mime == "image/gif") {
                 $image_o = imagecreatefromstring($image);
                 $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                 imagealphablending($image_p, false);
                 imagesavealpha($image_p, true);
                 imagealphablending($image_o, true);
                 imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height);
                 ob_start();
                 imagepng($image_p, null, 9);
                 $image = ob_get_contents();
                 ob_end_clean();
                 imagedestroy($image_o);
                 imagedestroy($image_p);
                 $ext = "png";
             } else {
                 if ($this->isGifImagesEnabled !== false && $mime == "image/gif") {
                     /*
                     $image_o = imagecreatefromstring($image);
                     $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                     
                     imagealphablending($image_p, false);
                     imagesavealpha($image_p, true);
                     imagealphablending($image_o, true);
                     
                     imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, ($width * $ratio), ($height * $ratio), $width, $height);
                     ob_start();
                     imagegif($image_p, null);
                     $image = ob_get_contents();
                     ob_end_clean();
                     
                     imagedestroy($image_o);
                     imagedestroy($image_p);
                     
                     $ext = "gif";
                     */
                     $tFileD = tempnam("BewareOfGeeksBearingGifs", "grD");
                     ResizeGif::ResizeByRatio($source, $tFileD, $ratio);
                     $image = file_get_contents($tFileD);
                     unlink($tFileD);
                 } else {
                     $image_o = imagecreatefromstring($image);
                     $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                     imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height);
                     ob_start();
                     imagejpeg($image_p, null, 80);
                     $image = ob_get_contents();
                     ob_end_clean();
                     imagedestroy($image_o);
                     imagedestroy($image_p);
                     $mime = "image/jpeg";
                     $ext = "jpg";
                 }
             }
         }
     }
     if ($ext === "") {
         static $mimeToExt = array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/png' => 'png', 'image/svg+xml' => "svg");
         if (isset($mimeToExt[$mime])) {
             $ext = $mimeToExt[$mime];
         }
     }
     $rv = array();
     $rv['width'] = $width * $ratio;
     $rv['height'] = $height * $ratio;
     $rv['mime'] = $mime;
     $rv['image'] = $image;
     $rv['ext'] = $ext;
     return $rv;
 }
Esempio n. 2
0
include "../vendor/autoload.php";
use grandt\ResizeGif\ResizeGif;
// Rotating_earth and SmallFullColourGIF are from the GIF Wikipedia page.
//// $srcFile = 'resources/Rotating_earth_(large).gif';
$srcFile = 'resources/SmallFullColourGIF.gif';
// $srcFile = 'resources/frame_diff_ir2t.gif';
$dstFile1 = 'resources/test1.gif';
$dstFile2 = 'resources/test2.gif';
$dstFile3 = 'resources/test3.gif';
echo "<!doctype html><html><head><title>ResizeGif.Test1</title></head><body>\n<p>Files are used for in and output, as you really don't want to\ndynamically resize animated gifs every time they are used.</p><pre>\n";
showStats($srcFile);
echo "Resizing to half size\n";
ResizeGif::ResizeByRatio($srcFile, $dstFile1, 0.5);
showStats($dstFile1);
echo "Resizing to 2x\n";
ResizeGif::ResizeByRatio($srcFile, $dstFile2, 2);
showStats($dstFile2);
echo "Resizing to 200px wide\n";
ResizeGif::ResizeToWidth($srcFile, $dstFile3, 200);
showStats($dstFile3);
echo "</pre></body></html>\n";
/**
 * @param string $file
 * @throws Exception
 */
function showStats($file)
{
    $size = ResizeGif::getSize($file);
    echo "File: {$file}:\n";
    echo " - width.: " . $size['width'] . "\n";
    echo " - height: " . $size['height'] . "\n";
Esempio n. 3
0
 /**
  * Get an image from a file or url, return it resized if the image exceeds the $maxImageWidth or $maxImageHeight directives.
  *
  * The return value is an array.
  * ['width'] is the width of the image.
  * ['height'] is the height of the image.
  * ['mime'] is the mime type of the image. Resized images are always in jpeg format.
  * ['image'] is the image data.
  * ['ext'] is the extension of the image file.
  *
  * @param string $source path or url to file.
  *
  * @return array|bool
  */
 function getImage($source)
 {
     $width = -1;
     $height = -1;
     $mime = "application/octet-stream";
     $ext = "";
     $image = $this->getFileContents($source);
     if ($image !== false && strlen($image) > 0) {
         $imageFile = imagecreatefromstring($image);
         if ($imageFile !== false) {
             $width = ImageSX($imageFile);
             $height = ImageSY($imageFile);
         }
         if ($this->isExifInstalled) {
             @($type = exif_imagetype($source));
             $mime = image_type_to_mime_type($type);
         }
         if ($mime === "application/octet-stream") {
             $mime = $this->image_file_type_from_binary($image);
         }
         if ($mime === "application/octet-stream") {
             $mime = $this->getMimeTypeFromUrl($source);
         }
     } else {
         return false;
     }
     if ($width <= 0 || $height <= 0) {
         return false;
     }
     $ratio = 1;
     if ($this->isGdInstalled) {
         if ($width > $this->maxImageWidth) {
             $ratio = $this->maxImageWidth / $width;
         }
         if ($height * $ratio > $this->maxImageHeight) {
             $ratio = $this->maxImageHeight / $height;
         }
         //echo "<pre>\$source: $source\n\$ratio.: $ratio\n\$mime..: $mime\n\$width.: $width\n\$height: $height</pre>\n";
         if ($ratio < 1 || empty($mime)) {
             if ($mime == "image/png" || $this->isGifImagesEnabled === false && $mime == "image/gif") {
                 $image_o = imagecreatefromstring($image);
                 $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                 imagealphablending($image_p, false);
                 imagesavealpha($image_p, true);
                 imagealphablending($image_o, true);
                 imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height);
                 ob_start();
                 imagepng($image_p, null, 9);
                 $image = ob_get_contents();
                 ob_end_clean();
                 imagedestroy($image_o);
                 imagedestroy($image_p);
                 $ext = "png";
             } else {
                 if ($this->isGifImagesEnabled !== false && $mime == "image/gif") {
                     /*
                     $image_o = imagecreatefromstring($image);
                     $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                     
                     imagealphablending($image_p, false);
                     imagesavealpha($image_p, true);
                     imagealphablending($image_o, true);
                     
                     imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, ($width * $ratio), ($height * $ratio), $width, $height);
                     ob_start();
                     imagegif($image_p, null);
                     $image = ob_get_contents();
                     ob_end_clean();
                     
                     imagedestroy($image_o);
                     imagedestroy($image_p);
                     
                     $ext = "gif";
                     */
                     $tFileD = tempnam("BewareOfGeeksBearingGifs", "grD");
                     ResizeGif::ResizeByRatio($source, $tFileD, $ratio);
                     $image = file_get_contents($tFileD);
                     unlink($tFileD);
                 } else {
                     $image_o = imagecreatefromstring($image);
                     $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio);
                     imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height);
                     ob_start();
                     imagejpeg($image_p, null, 80);
                     $image = ob_get_contents();
                     ob_end_clean();
                     imagedestroy($image_o);
                     imagedestroy($image_p);
                     $mime = "image/jpeg";
                     $ext = "jpg";
                 }
             }
         }
     }
     if ($ext === "") {
         static $mimeToExt = array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/png' => 'png');
         if (isset($mimeToExt[$mime])) {
             $ext = $mimeToExt[$mime];
         }
     }
     $rv = array();
     $rv['width'] = $width * $ratio;
     $rv['height'] = $height * $ratio;
     $rv['mime'] = $mime;
     $rv['image'] = $image;
     $rv['ext'] = $ext;
     return $rv;
 }