Example #1
0
 /**
  * Resizes image
  * @param string $src path to image
  * @param array $params resize parameters
  * @return array content and format
  */
 public static function resize($src, $params)
 {
     $img_functions = self::supportedFormats();
     $ext = $params['ext'];
     $new_width = $params['new_width'];
     $new_height = $params['new_height'];
     $dst_width = $params['dst_width'];
     $dst_height = $params['dst_height'];
     $width = $params['width'];
     $height = $params['height'];
     $bg_color = $params['bg_color'];
     $convert_to = $params['convert_to'];
     $jpeg_quality = $params['jpeg_quality'];
     $x = $params['x'];
     $y = $params['y'];
     $dst = imagecreatetruecolor($dst_width, $dst_height);
     if (function_exists('imageantialias')) {
         imageantialias($dst, true);
     }
     if ($ext == 'gif') {
         $new = imagecreatefromgif($src);
     } elseif ($ext == 'jpg') {
         $new = imagecreatefromjpeg($src);
     } elseif ($ext == 'png') {
         $new = imagecreatefrompng($src);
     }
     list($r, $g, $b) = empty($bg_color) ? fn_parse_rgb('#ffffff') : fn_parse_rgb($bg_color);
     $c = imagecolorallocate($dst, $r, $g, $b);
     if (empty($bg_color) && ($ext == 'png' || $ext == 'gif')) {
         if (function_exists('imagecolorallocatealpha') && function_exists('imagecolortransparent') && function_exists('imagesavealpha') && function_exists('imagealphablending')) {
             $c = imagecolorallocatealpha($dst, 255, 255, 255, 127);
             imagecolortransparent($dst, $c);
             imagesavealpha($dst, true);
             imagealphablending($dst, false);
         }
     }
     imagefilledrectangle($dst, 0, 0, $dst_width, $dst_height, $c);
     imagecopyresampled($dst, $new, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
     // Free memory from image
     imagedestroy($new);
     if ($convert_to == 'original') {
         $convert_to = $ext;
     } elseif (!empty($img_functions[$convert_to])) {
         $convert_to = $convert_to;
     } else {
         $convert_to = key($img_functions);
     }
     ob_start();
     if ($convert_to == 'gif') {
         imagegif($dst);
     } elseif ($convert_to == 'jpg') {
         imagejpeg($dst, null, $jpeg_quality);
     } elseif ($convert_to == 'png') {
         imagepng($dst);
     }
     $content = ob_get_clean();
     imagedestroy($dst);
     return array($content, $convert_to);
 }
Example #2
0
function fn_resize_image($src, $dest, $new_width = 0, $new_height = 0, $make_box = false, $bg_color = '#ffffff')
{
    static $notification_set = false;
    static $gd_settings = array();
    if (file_exists($src) && !empty($dest) && (!empty($new_width) || !empty($new_height)) && extension_loaded('gd')) {
        $img_functions = array('png' => function_exists('imagepng'), 'jpg' => function_exists('imagejpeg'), 'gif' => function_exists('imagegif'));
        if (empty($gd_settings)) {
            $gd_settings = fn_get_settings('Thumbnails');
        }
        $dst_width = $new_width;
        $dst_height = $new_height;
        list($width, $height, $mime_type) = fn_get_image_size($src);
        if (empty($width) || empty($height)) {
            return false;
        }
        if ($width < $new_width) {
            $new_width = $width;
        }
        if ($height < $new_height) {
            $new_height = $height;
        }
        if ($dst_height == 0) {
            // if we passed width only, calculate height
            $new_height = $dst_height = $height / $width * $new_width;
        } elseif ($dst_width == 0) {
            // if we passed height only, calculate width
            $new_width = $dst_width = $width / $height * $new_height;
        } else {
            // we passed width and height, limit image by height! (hm... not sure we need it anymore?)
            if ($new_width * $height / $width > $dst_height) {
                $new_width = $width * $dst_height / $height;
            }
            $new_height = $height / $width * $new_width;
            if ($new_height * $width / $height > $dst_width) {
                $new_height = $height * $dst_width / $width;
            }
            $new_width = $width / $height * $new_height;
        }
        $w = number_format($new_width, 0, ',', '');
        $h = number_format($new_height, 0, ',', '');
        $ext = fn_get_image_extension($mime_type);
        if (!empty($img_functions[$ext])) {
            if ($make_box) {
                $dst = imagecreatetruecolor($dst_width, $dst_height);
            } else {
                $dst = imagecreatetruecolor($w, $h);
            }
            if (function_exists('imageantialias')) {
                imageantialias($dst, true);
            }
        } elseif ($notification_set == false) {
            $msg = fn_get_lang_var('error_image_format_not_supported');
            $msg = str_replace('[format]', $ext, $msg);
            fn_set_notification('E', fn_get_lang_var('error'), $msg);
            $notification_set = true;
            return false;
        }
        if ($ext == 'gif' && $img_functions[$ext] == true) {
            $new = imagecreatefromgif($src);
        } elseif ($ext == 'jpg' && $img_functions[$ext] == true) {
            $new = imagecreatefromjpeg($src);
        } elseif ($ext == 'png' && $img_functions[$ext] == true) {
            $new = imagecreatefrompng($src);
        } else {
            return false;
        }
        // Set transparent color to white
        // Not sure that this is right, but it works
        // FIXME!!!
        // $c = imagecolortransparent($new);
        list($r, $g, $b) = fn_parse_rgb($bg_color);
        $c = imagecolorallocate($dst, $r, $g, $b);
        //imagecolortransparent($dst, $c);
        if ($make_box) {
            imagefilledrectangle($dst, 0, 0, $dst_width, $dst_height, $c);
            $x = number_format(($dst_width - $w) / 2, 0, ',', '');
            $y = number_format(($dst_height - $h) / 2, 0, ',', '');
        } else {
            imagefilledrectangle($dst, 0, 0, $w, $h, $c);
            $x = 0;
            $y = 0;
        }
        imagecopyresampled($dst, $new, $x, $y, 0, 0, $w, $h, $width, $height);
        if ($gd_settings['convert_to'] == 'original') {
            $gd_settings['convert_to'] = $ext;
        }
        if (empty($img_functions[$gd_settings['convert_to']])) {
            foreach ($img_functions as $k => $v) {
                if ($v == true) {
                    $gd_settings['convert_to'] = $k;
                    break;
                }
            }
        }
        switch ($gd_settings['convert_to']) {
            case 'gif':
                imagegif($dst, $dest);
                break;
            case 'jpg':
                imagejpeg($dst, $dest, $gd_settings['jpeg_quality']);
                break;
            case 'png':
                imagepng($dst, $dest);
                break;
        }
        @chmod($dest, DEFAULT_FILE_PERMISSIONS);
        return true;
    }
    return false;
}
Example #3
0
function fn_resize_image($src, $new_width = 0, $new_height = 0, $bg_color = '#ffffff')
{
    static $notification_set = false;
    static $gd_settings = array();
    if (file_exists($src) && (!empty($new_width) || !empty($new_height)) && extension_loaded('gd')) {
        $img_functions = array('png' => function_exists('imagepng'), 'jpg' => function_exists('imagejpeg'), 'gif' => function_exists('imagegif'));
        if (empty($gd_settings)) {
            $gd_settings = Settings::instance()->getValues('Thumbnails');
        }
        list($width, $height, $mime_type) = fn_get_image_size($src);
        if (empty($width) || empty($height)) {
            return false;
        }
        $ext = fn_get_image_extension($mime_type);
        if (empty($img_functions[$ext])) {
            if ($notification_set == false) {
                fn_set_notification('E', __('error'), __('error_image_format_not_supported', array('[format]' => $ext)));
                $notification_set = true;
            }
            return false;
        }
        if (empty($new_width) || empty($new_height)) {
            if ($width < $new_width) {
                $new_width = $width;
            }
            if ($height < $new_height) {
                $new_height = $height;
            }
        }
        $dst_width = $new_width;
        $dst_height = $new_height;
        if (empty($new_height)) {
            // if we passed width only, calculate height
            $dst_height = $new_height = $height / $width * $new_width;
        } elseif (empty($new_width)) {
            // if we passed height only, calculate width
            $dst_width = $new_width = $width / $height * $new_height;
        } else {
            // we passed width and height, we need to fit image in this sizes
            if ($new_width * $height / $width > $dst_height) {
                $new_width = $width * $dst_height / $height;
            }
            $new_height = $height / $width * $new_width;
            if ($new_height * $width / $height > $dst_width) {
                $new_height = $height * $dst_width / $width;
            }
            $new_width = $width / $height * $new_height;
            $make_box = true;
        }
        $new_width = intval($new_width);
        $new_height = intval($new_height);
        $dst = imagecreatetruecolor($dst_width, $dst_height);
        if (function_exists('imageantialias')) {
            imageantialias($dst, true);
        }
        if ($ext == 'gif') {
            $new = imagecreatefromgif($src);
        } elseif ($ext == 'jpg') {
            $new = imagecreatefromjpeg($src);
        } elseif ($ext == 'png') {
            $new = imagecreatefrompng($src);
        }
        list($r, $g, $b) = empty($bg_color) ? fn_parse_rgb('#ffffff') : fn_parse_rgb($bg_color);
        $c = imagecolorallocate($dst, $r, $g, $b);
        if (empty($bg_color) && ($ext == 'png' || $ext == 'gif')) {
            if (function_exists('imagecolorallocatealpha') && function_exists('imagecolortransparent') && function_exists('imagesavealpha') && function_exists('imagealphablending')) {
                $c = imagecolorallocatealpha($dst, 255, 255, 255, 127);
                imagecolortransparent($dst, $c);
                imagesavealpha($dst, true);
                imagealphablending($dst, false);
            }
        }
        imagefilledrectangle($dst, 0, 0, $dst_width, $dst_height, $c);
        if (!empty($make_box)) {
            $x = intval(($dst_width - $new_width) / 2);
            $y = intval(($dst_height - $new_height) / 2);
        } else {
            $x = 0;
            $y = 0;
        }
        imagecopyresampled($dst, $new, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
        // Free memory from image
        imagedestroy($new);
        if ($gd_settings['convert_to'] == 'original') {
            $convert_to = $ext;
        } elseif (!empty($img_functions[$gd_settings['convert_to']])) {
            $convert_to = $gd_settings['convert_to'];
        } else {
            $convert_to = key($img_functions);
        }
        ob_start();
        if ($convert_to == 'gif') {
            imagegif($dst);
        } elseif ($convert_to == 'jpg') {
            imagejpeg($dst, null, $gd_settings['jpeg_quality']);
        } elseif ($convert_to == 'png') {
            imagepng($dst);
        }
        $content = ob_get_clean();
        return array($content, $convert_to);
    }
    return false;
}