/** * liberty_generate_thumbnails * * @param array $pFileHash * @access public * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure */ function liberty_generate_thumbnails($pFileHash) { global $gBitSystem, $gThumbSizes; $resizeFunc = liberty_get_function('resize'); $ret = FALSE; // allow custom selection of thumbnail sizes if (empty($pFileHash['thumbnail_sizes'])) { if (!empty($gThumbSizes) && is_array($gThumbSizes)) { $pFileHash['thumbnail_sizes'] = array_keys($gThumbSizes); } else { $pFileHash['thumbnail_sizes'] = array('large', 'medium', 'small', 'avatar', 'icon'); } } if (!preg_match('#image/(gif|jpe?g|png)#i', $pFileHash['type']) && $gBitSystem->isFeatureActive('liberty_jpeg_originals') || in_array('original', $pFileHash['thumbnail_sizes'])) { // jpeg version of original if (preg_match('/pdf/i', $pFileHash['type'])) { // has a customer pdf rasterization function been defined? if (function_exists('liberty_rasterize_pdf') && ($rasteredFile = liberty_rasterize_pdf($pFileHash['source_file']))) { $pFileHash['source_file'] = $rasteredFile; } else { $magickWand = NewMagickWand(); if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand))) { MagickSetFormat($magickWand, 'JPG'); if (MagickGetImageColorspace($magickWand) == MW_CMYKColorspace) { MagickProfileImage($magickWand, "ICC", UTIL_PKG_PATH . 'icc/srgb.icm'); MagickSetImageColorspace($magickWand, MW_sRGBColorspace); } $imgWidth = MagickGetImageWidth($magickWand); $imgHeight = MagickGetImageHeight($magickWand); MagickSetImageUnits($magickWand, MW_PixelsPerInchResolution); MagickSetResolution($magickWand, 300, 300); $rasteredFile = dirname($pFileHash['source_file']) . '/original.jpg'; if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickWriteImage($magickWand, $rasteredFile), $magickWand))) { $pFileHash['source_file'] = $rasteredFile; } } } } else { $pFileHash['dest_base_name'] = 'original'; $pFileHash['name'] = 'original.jpg'; $pFileHash['max_width'] = MAX_THUMBNAIL_DIMENSION; $pFileHash['max_height'] = MAX_THUMBNAIL_DIMENSION; if ($convertedFile = $resizeFunc($pFileHash)) { $pFileHash['source_file'] = $convertedFile; $ret = TRUE; } } $pFileHash['type'] = $gBitSystem->verifyMimeType($pFileHash['source_file']); } // override $mimeExt if we have a custom setting for it if ($gBitSystem->isFeatureActive('liberty_thumbnail_format')) { $mimeExt = $gBitSystem->getConfig('liberty_thumbnail_format'); } else { list($type, $mimeExt) = preg_split('#/#', strtolower($pFileHash['type'])); } if (preg_match("!(png|gif)!", $mimeExt)) { $destExt = '.' . $mimeExt; } else { $destExt = '.jpg'; } $initialDestPath = $pFileHash['dest_branch']; foreach ($pFileHash['thumbnail_sizes'] as $thumbSize) { if (isset($gThumbSizes[$thumbSize])) { $pFileHash['dest_base_name'] = $thumbSize; $pFileHash['name'] = $thumbSize . $destExt; if (!empty($gThumbSizes[$thumbSize]['width'])) { $pFileHash['max_width'] = $gThumbSizes[$thumbSize]['width']; } else { // Have to unset since we reuse $pFileHash unset($pFileHash['max_width']); } // reset dest_branch for created thumbs if (!empty($pFileHash['thumb_path'])) { $pFileHash['dest_file'] = $pFileHash['thumb_path'] . $pFileHash['name']; } else { // create a subdirectory for the thumbs $pFileHash['dest_branch'] = $initialDestPath . 'thumbs/'; clearstatcache(); if (!is_dir(STORAGE_PKG_PATH . $pFileHash['dest_branch'])) { mkdir(STORAGE_PKG_PATH . $pFileHash['dest_branch'], 0775, TRUE); clearstatcache(); } } if (!empty($gThumbSizes[$thumbSize]['height'])) { $pFileHash['max_height'] = $gThumbSizes[$thumbSize]['height']; } else { // Have to unset since we reuse $pFileHash unset($pFileHash['max_height']); } if ($pFileHash['icon_thumb_path'] = $resizeFunc($pFileHash)) { $ret = TRUE; // use the previous thumb as the source for the next, decreasingly smaller thumb as this GREATLY increases speed $pFileHash['source_file'] = $pFileHash['icon_thumb_path']; } } } // to keep everything in bitweaver working smoothly, we need to remove the thumbs/ subdir again $pFileHash['dest_branch'] = $initialDestPath; return $ret; }
/** * liberty_magickwand_resize_image * * @param array $pFileHash * @access public * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure */ function liberty_magickwand_resize_image(&$pFileHash) { global $gBitSystem; // static var here is crucial static $rgbConverts = array(); $magickWand = NewMagickWand(); $pFileHash['error'] = NULL; $ret = NULL; if (!empty($pFileHash['source_file']) && is_file($pFileHash['source_file']) && filesize($pFileHash['source_file'])) { if ($error = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand)) { // $pFileHash['error'] = $error; $destFile = liberty_process_generic($pFileHash, FALSE); } else { if (MagickGetImageColorspace($magickWand) == MW_CMYKColorspace) { // These two lines are a hack needed for version of Ghostscript less that 8.60 MagickRemoveImageProfile($magickWand, "ICC"); MagickSetImageProfile($magickWand, 'ICC', file_get_contents(UTIL_PKG_PATH . 'icc/USWebCoatedSWOP.icc')); MagickProfileImage($magickWand, 'ICC', file_get_contents(UTIL_PKG_PATH . 'icc/srgb.icm')); MagickSetImageColorspace($magickWand, MW_RGBColorspace); $pFileHash['colorspace_conversion'] = TRUE; } MagickSetImageCompressionQuality($magickWand, $gBitSystem->getConfig('liberty_thumbnail_quality', 85)); $iwidth = round(MagickGetImageWidth($magickWand)); $iheight = round(MagickGetImageHeight($magickWand)); // this does not seem to be needed. magickwand will work out what to do by using the destination file extension //MagickSetImageFormat( $magickWand, $format ); if (empty($pFileHash['max_width']) && empty($pFileHash['max_height']) || !empty($pFileHash['max_width']) && $pFileHash['max_width'] == MAX_THUMBNAIL_DIMENSION || !empty($pFileHash['max_height']) && $pFileHash['max_height'] == MAX_THUMBNAIL_DIMENSION) { $pFileHash['max_width'] = $iwidth; $pFileHash['max_height'] = $iheight; } elseif ($iheight && $iwidth / $iheight < 1 && !empty($pFileHash['max_width']) && !empty($pFileHash['max_height'])) { // we have a portrait image, flip everything $temp = $pFileHash['max_width']; $pFileHash['max_height'] = $pFileHash['max_width']; $pFileHash['max_width'] = round($iwidth / $iheight * $pFileHash['max_height']); } elseif (!empty($pFileHash['max_width'])) { $pFileHash['max_height'] = round($iheight / $iwidth * $pFileHash['max_width']); } elseif (!empty($pFileHash['max_height'])) { $pFileHash['max_width'] = round($iwidth / $iheight * $pFileHash['max_height']); } // Make sure not to scale up if ($pFileHash['max_width'] > $iwidth && $pFileHash['max_height'] > $iheight) { $pFileHash['max_width'] = $iwidth; $pFileHash['max_height'] = $iheight; } // override $mimeExt if we have a custom setting for it if ($gBitSystem->isFeatureActive('liberty_thumbnail_format')) { $mimeExt = $gBitSystem->getConfig('liberty_thumbnail_format'); } elseif ($itype = MagickGetImageMimeType($magickWand)) { list($type, $mimeExt) = preg_split('#/#', strtolower($itype)); } else { list($type, $mimeExt) = preg_split('#/#', strtolower($pFileHash['type'])); } $replaced = FALSE; $mimeExt = preg_replace("!^(x-)?(jpeg|png|gif)\$!", "\$2", $mimeExt, -1, $replaced); if ($replaced) { $targetType = $mimeExt; $destExt = '.' . $mimeExt; } if (empty($destExt) || $mimeExt == 'jpeg') { $targetType = 'jpeg'; $destExt = '.jpg'; } if (!empty($pFileHash['dest_file'])) { $destFile = $pFileHash['dest_file']; } else { $destFile = STORAGE_PKG_PATH . $pFileHash['dest_branch'] . $pFileHash['dest_base_name'] . $destExt; } if (!empty($pFileHash['max_width']) && !empty($pFileHash['max_height']) && ($pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight || $mimeExt != $targetType) || !empty($pFileHash['colorspace_conversion'])) { $pFileHash['name'] = $pFileHash['dest_base_name'] . $destExt; // Alternate Filter settings can seen here http://www.dylanbeattie.net/magick/filters/result.html if ($error = liberty_magickwand_check_error(MagickResizeImage($magickWand, $pFileHash['max_width'], $pFileHash['max_height'], MW_CatromFilter, 1.0), $magickWand)) { $pFileHash['error'] .= $error; } if ($error = liberty_magickwand_check_error(MagickWriteImage($magickWand, $destFile), $magickWand)) { $pFileHash['error'] .= $error; } $pFileHash['size'] = filesize($destFile); } else { copy($pFileHash['source_file'], $destFile); } } $ret = $destFile; } else { $pFileHash['error'] = "No source file to resize"; } DestroyMagickWand($magickWand); return $ret; }
function resizeMobile2($src_path, $dest_path, $d_width, $d_height) { $mk = NewMagickWand(); if (!MagickPingImage($mk, $src_path)) { echo "magick wand - no image \n"; $format = sprintf("convert %s -resize %dx%d -colors 256 -quality 90 -depth 8 %s", $src_path, $destWidth, $destHeight, $dest_path); $buffer = ""; exec($format, $buffer); return false; } // Now we need to clear out the data that MagickPingImage() put there ClearMagickWand($mk); if (MagickReadImage($mk, $src_path)) { list($srcWidth, $srcHeight, $destWidth, $destHeight) = getRate($src_path, $d_width, $d_height); //소스 이미지를 읽어서 $mk = MagickTransformImage($mk, NULL, $destWidth . "x" . $destHeight); MagickSetImageCompressionQuality($mk, 90); MagickSetImageDepth($mk, 8); //MagickSetImageIndex($mk, 256); MagickProfileImage($mk, "*", ""); MagickQuantizeImage($mk, 256, MW_RGBColorspace, 0, true, false); //$chk = MagickResizeImage($mk, $destWidth, $destHeight); //echo "$src_path , $dest_path, $destWidth, $destHeight \n"; // 이미지를 리사이징해라. 가로 $w 세로 $h //MagickResizeImage() 이라는 함수도 있는데 위의 것이 더 범용적입니다. if ($mk == null) { //echo "this is convert"; $format = sprintf("convert %s -resize %dx%d -colors 256 -quality 90 -depth 8 %s", $src_path, $destWidth, $destHeight, $dest_path); $buffer = ""; exec($format, $buffer); //echo "object is null \n"; return true; } MagickWriteImage($mk, $dest_path); // 새로운 이미지를 만들어라~ ClearMagickWand($mk); } else { echo "magick wand - read fail \n"; return false; } return true; }