Example #1
0
function makethumb($srcFile, $dstFile, $dstW, $dstH, $imwf = false)
{
    $im = new imagick($srcFile);
    $source_w = $im->getImageWidth();
    $source_h = $im->getImageHeight();
    if ($dstW === 0 && $dstH === 0) {
        $dstW = $source_w;
        $dstH = $source_h;
    } else {
        // 取得縮在此範圍內的比例
        $percent = getResizePercent($source_w, $source_h, $dstW, $dstH);
        $dstW = $source_w * $percent;
        $dstH = $source_h * $percent;
    }
    $im->thumbnailImage($dstW, $dstH);
    $im->writeImage($dstFile);
    $watermarkFile = './logo.png';
    if ($imwf && is_file($watermarkFile) && is_file($dstFile)) {
        $image = new imagick($dstFile);
        $watermark = new imagick($watermarkFile);
        $iWidth = $image->getImageWidth();
        $iHeight = $image->getImageHeight();
        $wWidth = $watermark->getImageWidth();
        $wHeight = $watermark->getImageHeight();
        if ($iHeight < $wHeight || $iWidth < $wWidth) {
            $watermark->scaleImage($iWidth, $iHeight);
            $wWidth = $watermark->getImageWidth();
            $wHeight = $watermark->getImageHeight();
        }
        $x = ($iWidth - $wWidth) / 2;
        $y = ($iHeight - $wHeight) / 2;
        $image->compositeImage($watermark, imagick::COMPOSITE_OVER, $x, $y);
        $image->writeImage($dstFile);
    }
}
Example #2
0
/**
The MIT License
*
* 抓取要縮圖的比例, 下述只處理 jpeg
* $from_filename : 來源路徑, 檔名, ex: /tmp/xxx.jpg
* $save_filename : 縮圖完要存的路徑, 檔名, ex: /tmp/ooo.jpg
* $in_width : 縮圖預定寬度
* $in_height: 縮圖預定高度
* $quality  : 縮圖品質(1~100)
*
* Usage:
*   ImageResize('ram/xxx.jpg', 'ram/ooo.jpg');
*/
function ImageResize($from_filename, $save_filename, $in_width = 9999, $in_height = 9999, $quality = 100)
{
    $allow_format = array('jpeg', 'png', 'gif');
    $sub_name = $t = '';
    // Get new dimensions
    $img_info = getimagesize($from_filename);
    $width = $img_info['0'];
    $height = $img_info['1'];
    $imgtype = $img_info['2'];
    $imgtag = $img_info['3'];
    $bits = $img_info['bits'];
    $channels = $img_info['channels'];
    $mime = $img_info['mime'];
    if ($width > 1500 || $height > 1500) {
        return false;
        exit;
    }
    list($t, $sub_name) = split('/', $mime);
    if ($sub_name == 'jpg') {
        $sub_name = 'jpeg';
    }
    if (!in_array($sub_name, $allow_format)) {
        return false;
    }
    // 取得縮在此範圍內的比例
    $percent = getResizePercent($width, $height, $in_width, $in_height);
    $new_width = $width * $percent;
    $new_height = $height * $percent;
    // Resample
    $image_new = imagecreatetruecolor($new_width, $new_height);
    // $function_name: set function name
    //   => imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif
    /*
    // $sub_name = jpeg, png, gif
    $function_name = 'imagecreatefrom'.$sub_name;
    $image = $function_name($filename); //$image = imagecreatefromjpeg($filename);
    */
    if ($sub_name == "png") {
        $image = imagecreatefrompng($from_filename);
        imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        return imagepng($image_new, $save_filename, $quality);
    } else {
        if ($sub_name == "gif") {
            $image = imagecreatefromgif($from_filename);
            imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            return imagegif($image_new, $save_filename, $quality);
        } else {
            $image = imagecreatefromjpeg($from_filename);
            imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            return imagejpeg($image_new, $save_filename, $quality);
        }
    }
}
Example #3
0
 $save_filename = 'upload/' . $gdir . '/';
 if ($save_filename && !is_dir($save_filename)) {
     mkdir($save_filename, 0777, true);
 } else {
     $file = strtolower(basename($url));
     if (is_file($save_filename . $file) != NULL && filemtime($save_filename . $file) >= strtotime("-3 days")) {
         $check_img = 1;
         $image = open_image($save_filename . $file);
         imageinterlace($image, 1);
         //--將圖片轉換為漸進式模
         imagejpeg($image);
     }
 }
 if ($check_img != 1) {
     //縮圖比例
     $percent = getResizePercent($w, $h, $new_w, $new_h);
     //比預設縮圖小就不用縮
     if ($percent == 1) {
         imagejpeg($image, $save_filename . strtolower(basename($url)));
         imageinterlace($image, 1);
         //--將圖片轉換為漸進式模
         imagejpeg($image);
     } else {
         $new_w = $w * $percent;
         $new_h = $h * $percent;
         //建立縮圖圖片
         $im2 = ImageCreateTrueColor($new_w, $new_h);
         imagecopyResampled($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
         $image = $im2;
         imagejpeg($image, $save_filename . strtolower(basename($url)));
         imageinterlace($image, 1);
Example #4
0
 function ImageResize($from_filename, $save_filename, $in_width = 1024, $in_height = 1024, $quality = 100)
 {
     $allow_format = array('jpeg', 'png', 'gif');
     $sub_name = $t = '';
     // Get new dimensions
     $img_info = getimagesize($from_filename);
     $width = $img_info['0'];
     $height = $img_info['1'];
     $imgtype = $img_info['2'];
     $imgtag = $img_info['3'];
     $bits = $img_info['bits'];
     $channels = $img_info['channels'];
     $mime = $img_info['mime'];
     list($t, $sub_name) = split('/', $mime);
     if ($sub_name == 'jpg') {
         $sub_name = 'jpeg';
     }
     /*
         if (!in_array($sub_name, $allow_format)) {
             return false;
         }
     */
     // 取得縮在此範圍內的比例
     $percent = getResizePercent($width, $height, $in_width, $in_height);
     if ($percent == 1) {
         return move_uploaded_file($from_filename, $save_filename);
     }
     $new_width = $width * $percent;
     $new_height = $height * $percent;
     // Resample
     $image_new = imagecreatetruecolor($new_width, $new_height);
     // $function_name: set function name
     //   => imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif
     /*
     // $sub_name = jpeg, png, gif
     $function_name = 'imagecreatefrom' . $sub_name;
     
     if ($sub_name=='png')
         return $function_name($image_new, $save_filename, intval($quality / 10 - 1));
     
     $image = $function_name($from_filename); //$image = imagecreatefromjpeg($from_filename);
     */
     $image = imagecreatefromjpeg($from_filename);
     imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     imageinterlace($image_new, 1);
     //--將圖片轉換為漸進式模式
     return imagejpeg($image_new, $save_filename, $quality);
     imagedestroy($im);
 }