Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Miscellany
Tuesday, August 7th, 2007 at 9:07:59am MDT 

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // |Image Handler for zen-cart Open Source E-commerce                     |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2005 Tim Kroeger                                       |
  7. // | http://www.breakmyzencart.com                                        |
  8. // |                                                                      |
  9. // |                                                                      |
  10. // |                                                                      |
  11. // +----------------------------------------------------------------------+
  12. // | This source file is subject to version 2.0 of the GPL license,       |
  13. // | that is bundled with this package in the file LICENSE.txt, and is    |
  14. // | available through the world-wide-web at the following url:           |
  15. // | http://www.gnu.org/licenses/gpl.txt                                  |
  16. // | If you did not receive a copy of the license and are unable          |
  17. // | to obtain it through the world-wide-web, please send a note to       |
  18. // | tim@breakmyzencart.com so I can mail you a copy immediately.         |
  19. // +----------------------------------------------------------------------+
  20. // $Id: functions_images.php,v 1.1.2.18 2005/05/21 20:59:23 tim Exp $
  21.  
  22. /**
  23.  * Manage resizing, retrieving and on-the-fly watermarking of
  24.  * (product) images
  25.  * @author      Tim Kroeger <tim@breakmyzencart.com>
  26.  * @version     1.0-beta2
  27.  */
  28.  
  29. /**
  30.  * set the environment for a user-installed ImageMagick distribution
  31.  */
  32. function zen_imagemagick_setenv() {
  33.         $success = true;
  34.         if (zen_not_null(DIR_FS_IMAGE_MAGICK_LIB)) {
  35.                 $imagemagick_lib = ereg_replace(DIRECTORY_SEPARATOR . '$', '', DIR_FS_IMAGE_MAGICK_LIB);
  36.                 // update LD_LIBRARY_PATH if needed
  37.                 $saved_ld = getenv('LD_LIBRARY_PATH');
  38.                 $new_ld = $imagemagick_lib;
  39.                 if (zen_not_null($saved_ld) && (!strstr($saved_ld, $new_ld))) {
  40.                         $new_ld .= ':' . $saved_ld;
  41.                 }
  42.                 $succes = putenv('LD_LIBRARY_PATH=' . $new_ld);
  43.                 $imagemagick_home = ereg_replace(DIRECTORY_SEPARATOR . 'lib$', '', $imagemagick_lib);
  44.                 $success = $success && putenv('MAGICK_HOME=' . $imagemagick_home);
  45.         }
  46.         return $success;
  47. }
  48.  
  49. /**
  50.  * get the environment for a user-installed ImageMagick distribution
  51.  */
  52. function zen_imagemagick_getenv() {
  53.         $im_env = '';
  54.         if (zen_not_null(DIR_FS_IMAGE_MAGICK_LIB)) {
  55.                 $imagemagick_lib = ereg_replace(DIRECTORY_SEPARATOR . '$', '', DIR_FS_IMAGE_MAGICK_LIB);
  56.                 // update LD_LIBRARY_PATH if needed
  57.                 $saved_ld = getenv('LD_LIBRARY_PATH');
  58.                 $new_ld = $imagemagick_lib;
  59.                 if (zen_not_null($saved_ld) && (!strstr($saved_ld, $new_ld))) {
  60.                         $new_ld .= ':' . $saved_ld;
  61.                 }
  62.                 $imagemagick_home = ereg_replace(DIRECTORY_SEPARATOR . 'lib$', '', $imagemagick_lib);
  63.                 $im_env = 'LD_LIBRARY_PATH="' . $new_ld . '" MAGICK_HOME="' . $imagemagick_home . '" ';
  64.         }
  65.         return $im_env;
  66. }
  67.  
  68. /**
  69.  * return version information of the specified image handler
  70.  */
  71. function zen_get_imagehandler_version() {
  72.         $info = '';
  73.         if (IMAGE_MANAGER_HANDLER == 'GD') {
  74.                 $gd_info = @gd_info();
  75.                 $info = 'GD ' . $gd_info['GD Version'];
  76.         } elseif (IMAGE_MANAGER_HANDLER == 'ImageMagick') {
  77.                 if ((is_file(DIR_FS_IMAGE_MAGICK . 'convert')) || (is_file(DIR_FS_IMAGE_MAGICK . 'convert.exe'))) {
  78.                         zen_imagemagick_setenv();
  79.                         $command = DIR_FS_IMAGE_MAGICK . 'convert -version 2>&1';
  80.                         exec($command, $im_info);
  81.                         $version = split(' ', implode(' ', $im_info));
  82.                         $info = $version[1] . ' ' . $version[2] . ' ' . $version[3];
  83.                         //$info = $command . '<br />' . implode('<br/>', $im_info);
  84.                 } else {
  85.                         zen_add_message(ERROR_IMAGE_MAGICK_NOT_FOUND, 'error');
  86.                 }
  87.         }
  88.         return $info;
  89. }
  90.  
  91. /**
  92.  * Use GD library to resize image proportionally
  93.  */
  94. function zen_gd_resize_image($source_name, $destination_name, $background, $newwidth = -1, $newheight = -1, $quality = 75) {
  95.         list($width, $height) = getimagesize($source_name);
  96.         if (($newwidth > 0) && ($newheight > 0)) {
  97.                 $canvaswidth = $newwidth;
  98.                 $canvasheight = $newheight;
  99.                 $scale = max($height / $newheight, $width / $newwidth);
  100.                 $newwidth = round($width / $scale);
  101.                 $newheight = round($height / $scale);
  102.                 $startwidth = (($canvaswidth - $newwidth)/2);
  103.                 $startheight = (($canvasheight - $newheight)/2);
  104.         } else {
  105.                 $canvaswidth  = $newwidth  = $width;
  106.                 $canvasheight = $newheight = $height;
  107.         }
  108.         $source = zen_load_gd_image($source_name);
  109.         $destination = zen_create_gd_image($canvaswidth, $canvasheight, $background);
  110.         imagealphablending($destination, true);
  111.         $image_extension = strtolower(substr($destination_name, strrpos($destination_name, '.')));
  112.         $transparent = ((!(strpos($background, 'transparent')) === FALSE)
  113.                                                 && (($image_extension == '.gif')
  114.                                                 || ($image_extension == '.png')));
  115.         if ($transparent) {
  116.                 imagecopyresized($destination, $source, $startwidth, $startheight, 0, 0, $newwidth, $newheight, $width, $height);
  117.                 //imagecopyresampled gives strange results on transparent backgrounds/images.
  118.                 //imagecopyresampled($destination, $source, $startwidth, $startheight, 0, 0, $newwidth, $newheight, $width, $height);
  119.         } else {
  120.                 imagecopyresampled($destination, $source, $startwidth, $startheight, 0, 0, $newwidth, $newheight, $width, $height);
  121.         }
  122.         imagedestroy($source);
  123.         zen_save_gd_image($destination, $destination_name, $quality);
  124.         imagedestroy($destination);
  125.  
  126. }
  127.  
  128. /**
  129.  * Render image at specified dimensions
  130.  */
  131. function zen_imagemagick_render_image($source_name, $destination_name, $background, $size, $quality) {
  132.         global $messageStack;
  133.         zen_imagemagick_setenv();
  134.         bmz_set_visible($source_name);
  135.         bmz_set_visible($destination_name);
  136.         $command  = DIR_FS_IMAGE_MAGICK . "convert -size " . $size . " xc:none -fill " . $background . " -draw 'color 0,0 reset' " . $source_name;
  137.         $command .= " -compose Over -gravity Center -geometry " . $size . " -composite -quality " . $quality . " " . $destination_name;   
  138.         exec($command . ' 2>&1', $message);
  139.         if ($message) zen_add_message(sprintf(ERROR_EXECUTING_IMAGE_MAGICK, $command) . implode('<br/>', $message), 'error');
  140.         $message = null;
  141.         bmz_set_visible($destination_name);
  142. }
  143.  
  144. /**
  145.  * Use ImageMagick to resize image proportionally
  146.  */
  147. function zen_imagemagick_resize_image($source_name, $destination_name, $background, $size, $quality = 75) {
  148.         $background = trim($background);
  149.         // if $background string contains no 'transparent' substring
  150.         if (strpos($background, 'transparent') === FALSE) {
  151.                 zen_imagemagick_render_image($source_name, $destination_name, $background, $size, $quality);
  152.         } else {
  153.                 $background = ereg_replace('transparent', '', $background);
  154.                 $background = trim($background);
  155.                 // special treatment for smooth transparent gif to background color rendering
  156.                 $destination_image_extension =  substr($destination_name, strrpos($destination_name, '.'));
  157.                 if ($destination_image_extension == '.gif') {
  158.                         $background = ($background != '') ? $background : 'white';
  159.                         $threshold = "20%"; // threshold where semitransparent pixels turn to background color.
  160.                         zen_imagemagick_render_gif($source_name, $destination_name, $background, $size, $threshold);
  161.                 } else {
  162.                         zen_imagemagick_render_image($source_name, $destination_name, 'transparent', $size, $quality);
  163.                 }
  164.         }
  165. }
  166.  
  167. /**
  168.  * Calculate desired image size as set in admin->configuration->images.
  169.  */
  170. function zen_calculate_image_size($image_name, $pref_width, $pref_height = '') {
  171.         list($width, $height) = @getimagesize($image_name);
  172.         $width = intval($width);
  173.         $height = intval($height);
  174.         // default: nothing happens (preferred dimension = actual dimension)
  175.         $newwidth = $width;
  176.         $newheight = $height;
  177.         if (($width > 0) && ($height > 0)) {
  178.                 if (!(strrpos($pref_width . $pref_height, '%') === FALSE)) {
  179.                         // possible scaling to % of original size
  180.                         // calculate new dimension in pixels
  181.                         $scale = intval($pref_width . $pref_height) / 100;
  182.                         $newwidth = floor($width * $scale);
  183.                         $newheight = floor($height * $scale);
  184.                 } else {
  185.                         $force_canvas_width = !(strrpos($pref_width . $pref_height, '!') === FALSE);
  186.                         // failsafe for old zen-cart configuration one image dimension set to 0
  187.                         $pref_width = ($pref_width == '' || intval($pref_width) == 0) ? 0 : intval($pref_width);
  188.                         $pref_height = ($pref_height == '' || intval($pref_height) == 0) ? 0 : intval($pref_height);
  189.                         if ((!$force_canvas_width) && ($pref_width != 0) && ($pref_height != 0)) {
  190.                                 // if no '!' is appended to dimensions we don't force the canvas size to
  191.                                 // match the preferred size. the image will not have the exact specified size.
  192.                                 // (we're in fact forcing the old 0-dimension zen-magic trick)
  193.                                 $oldratio = $width / $height;
  194.                                 $pref_ratio = $pref_width / $pref_height;
  195.                                 if ($pref_ratio > $oldratio) {
  196.                                         $pref_width = 0;
  197.                                 } else {
  198.                                         $pref_height = 0;
  199.                                 }
  200.                         }
  201.                         // now deal with the calculated preferred sizes
  202.                         if (($pref_width == 0) && ($pref_height > 0)) {
  203.                                 // image dimensions are calculated to fit the preferred height
  204.                                 $pref_width = floor($width / ($height / $pref_height));
  205.                         } elseif (($pref_width > 0) && ($pref_height == 0)) {
  206.                                 // image dimensions are calculated to fit the preferred width
  207.                                 $pref_height = floor($height / ($width / $pref_width));
  208.                         }
  209.                         if ((($pref_width > 0) && ($pref_height > 0))
  210.                                 && (($pref_width < $width ) || ($pref_height < $height))) {
  211.                                 // only calculate new dimensions if image is larger than
  212.                                 // preferred size, do not calculate dimensions larger than original
  213.                                 $newwidth = $pref_width;
  214.                                 $newheight = $pref_height;
  215.                         }
  216.                 }
  217.         }
  218.         $resize = (($newwidth != $width) || ($newheight != $height));
  219.         return array($newwidth, $newheight, $resize);
  220. }
  221.  
  222. /**
  223.  * get image string for large images. honor wishes for watermarked images.
  224.  * if no large image available, get medium.
  225.  */
  226. function zen_get_large_image($image_base, $image_extension) {
  227.         $file_extension = ((LARGE_IMAGE_FILETYPE == 'no_change') ? $image_extension : '.' . LARGE_IMAGE_FILETYPE);
  228.         if (WATERMARK_LARGE_IMAGES == 'True') {
  229.                 $image = DIR_WS_IMAGES . 'large/watermark/' . $image_base . IMAGE_SUFFIX_LARGE . $file_extension;
  230.                 $unmarked_image = DIR_WS_IMAGES . 'large/' . $image_base . IMAGE_SUFFIX_LARGE . $file_extension;
  231.                 if (!file_exists(DIR_FS_CATALOG . $image)) {
  232.                         bmz_set_visible(DIR_FS_CATALOG . $unmarked_image);
  233.                         if (!file_exists(DIR_FS_CATALOG . $unmarked_image)) {
  234.                                 $image = zen_get_medium_image($image_base, $image_extension);
  235.                         } elseif (IMAGE_MANAGER_HANDLER && (IMAGE_MANAGER_HANDLER != 'none')) {
  236.                                 zen_create_marked_large_image($image_base, $file_extension);
  237.                         } else {
  238.                                 $image = $unmarked_image;
  239.                         }
  240.                 } else {
  241.                         $marked_image_mtime = filemtime(DIR_FS_CATALOG . $image);
  242.                         bmz_set_visible(DIR_FS_CATALOG . $unmarked_image);
  243.                         if ((file_exists($unmarked_image))
  244.                                         && ((@filemtime(DIR_FS_CATALOG . $unmarked_image) > $marked_image_mtime)
  245.                                         || (@filemtime(DIR_FS_CATALOG . DIR_WS_IMAGES . 'large/watermark_LRG.png') > $marked_image_mtime))) {
  246.                                 zen_create_marked_large_image($image_base, $file_extension);
  247.                         }
  248.                         bmz_set_invisible(DIR_FS_CATALOG . $unmarked_image);
  249.                 }
  250.         } else {
  251.                 $image = DIR_WS_IMAGES . 'large/' . $image_base . IMAGE_SUFFIX_LARGE . $file_extension;
  252.                 bmz_set_visible(DIR_FS_CATALOG . $image);
  253.                 if (!file_exists(DIR_FS_CATALOG . $image)) {
  254.                         $image = zen_get_medium_image($image_base, $image_extension);
  255.                 }
  256.         }
  257.         return $image;
  258. }
  259.  
  260. /**
  261.  * get image string for medium images. honor wishes for watermarked images.
  262.  * if no medium image available, get small.
  263.  */
  264. function zen_get_medium_image($image_base, $image_extension) {
  265.         $file_extension = ((MEDIUM_IMAGE_FILETYPE == 'no_change') ? $image_extension : '.' . MEDIUM_IMAGE_FILETYPE);
  266.         if (WATERMARK_MEDIUM_IMAGES == 'True') {
  267.                 $image = DIR_WS_IMAGES . 'medium/watermark/' . $image_base . IMAGE_SUFFIX_MEDIUM . $file_extension;
  268.                 $unmarked_image = DIR_WS_IMAGES . 'medium/' . $image_base . IMAGE_SUFFIX_MEDIUM . $file_extension;
  269.                 if (!file_exists(DIR_FS_CATALOG . $image)) {
  270.                         bmz_set_visible(DIR_FS_CATALOG . $unmarked_image);
  271.                         if (!file_exists(DIR_FS_CATALOG . $unmarked_image)) {
  272.                                 $image = zen_get_small_image($image_base . $image_extension);
  273.                         } elseif (IMAGE_MANAGER_HANDLER && (IMAGE_MANAGER_HANDLER != 'none')) {
  274.                                 zen_create_marked_medium_image($image_base, $file_extension);
  275.                         } else {
  276.                                 $image = $unmarked_image;
  277.                         }
  278.                 } else {
  279.                         $marked_image_mtime = filemtime(DIR_FS_CATALOG . $image);
  280.                         bmz_set_visible(DIR_FS_CATALOG . $unmarked_image);
  281.                         if ((file_exists($unmarked_image))
  282.                                         && ((@filemtime(DIR_FS_CATALOG . $unmarked_image) > $marked_image_mtime)
  283.                                         || (@filemtime(DIR_FS_CATALOG . DIR_WS_IMAGES . 'medium/watermark_MED.png') > $marked_image_mtime))) {
  284.                                 zen_create_marked_medium_image($image_base, $file_extension);
  285.                         }
  286.                         bmz_set_invisible(DIR_FS_CATALOG . $unmarked_image);
  287.                 }
  288.         } else {
  289.                 $image = DIR_WS_IMAGES . 'medium/' . $image_base . IMAGE_SUFFIX_MEDIUM . $file_extension;
  290.                 bmz_set_visible(DIR_FS_CATALOG . $image);
  291.                 if (!file_exists(DIR_FS_CATALOG . $image)) {
  292.                         $image = zen_get_small_image($image_base .$image_extension);
  293.                 }
  294.         }
  295.         return $image;
  296. }
  297.  
  298. /**
  299.  * get image string for small images. honor wishes for watermarked images.
  300.  * if no small image available, return an empty string (maybe NULL is better?).
  301.  */
  302. function zen_get_small_image($image_name) {
  303.         if (WATERMARK_SMALL_IMAGES == 'True') {
  304.                 $image = DIR_WS_IMAGES . 'watermark/' . $image_name;
  305.                 $unmarked_image = DIR_WS_IMAGES . $image_name;
  306.                 if (!file_exists(DIR_FS_CATALOG . $image)) {
  307.                         bmz_set_visible(DIR_FS_CATALOG . $unmarked_image);
  308.                         if (!file_exists(DIR_FS_CATALOG . $unmarked_image)) {
  309.                                 $image = DIR_WS_IMAGES;
  310.                         } elseif (IMAGE_MANAGER_HANDLER && (IMAGE_MANAGER_HANDLER != 'none')) {
  311.                                 zen_create_marked_small_image($image_name);
  312.                         } else {
  313.                                 $image = $unmarked_image;
  314.                         }
  315.                 } else {
  316.                         $marked_image_mtime = filemtime(DIR_FS_CATALOG . $image);
  317.                         bmz_set_visible(DIR_FS_CATALOG . $unmarked_image);
  318.                         if ((file_exists($unmarked_image))
  319.                                         && ((@filemtime(DIR_FS_CATALOG . $unmarked_image) > $marked_image_mtime)
  320.                                         || (@filemtime(DIR_FS_CATALOG . DIR_WS_IMAGES . 'watermark.png') > $marked_image_mtime))) {
  321.                                 zen_create_marked_small_image($image_name);
  322.                         }
  323.                         bmz_set_invisible(DIR_FS_CATALOG . $unmarked_image);
  324.                 }
  325.         } else {
  326.                 $image = DIR_WS_IMAGES . $image_name;
  327.                 bmz_set_visible(DIR_FS_CATALOG . $image);
  328.         }
  329.         return $image;
  330. }
  331.  
  332. /**
  333.  * Use the appropriate GD function to create an image from filename.
  334.  */
  335. function zen_load_gd_image($source_name) {
  336.         bmz_set_visible($source_name);
  337.         $image_extension = substr($source_name, strrpos($source_name, '.'));
  338.         switch (strtolower($image_extension)) {
  339.                 case '.gif':
  340.                         // load and put into rgb colorspace
  341.                         list($width, $height) = getimagesize($source_name);
  342.                         $gif_image = imagecreatefromgif($source_name);
  343.                         $image = zen_create_gd_image($width, $height);
  344.                         imagealphablending($image, true);
  345.                         imagecopy($image, $gif_image, 0, 0, 0, 0, $width, $height);
  346.                         break;
  347.                 case '.png':
  348.                         $image = imagecreatefrompng($source_name);
  349.                         break;
  350.                 case '.jpg':
  351.                         $image = imagecreatefromjpeg($source_name);
  352.                         break;
  353.                 case '.jpeg':
  354.                         $image = imagecreatefromjpeg($source_name);
  355.                         break;
  356.         }
  357.         return $image;
  358. }
  359.  
  360. /**
  361.  * Use the appropriate GD function to save an image to the filesystem
  362.  */
  363. function zen_save_gd_image($image, $destination_name, $quality = 75) {
  364.         bmz_set_visible($destination_name);
  365.         $image_extension = substr($destination_name, strrpos($destination_name, '.'));
  366.         switch (strtolower($image_extension)) {
  367.                 case '.gif':
  368.                         imagetruecolortopalette($image, false, 256);
  369.                         imagegif($image, $destination_name);
  370.                         break;
  371.                 case '.png': imagepng($image, $destination_name, $quality); break;
  372.                 case '.jpg': imagejpeg($image, $destination_name, $quality); break;
  373.                 case '.jpeg': imagejpeg($image, $destination_name, $quality); break;
  374.         }
  375.         bmz_set_visible($destination_name);
  376. }
  377.  
  378. /**
  379.  * Use GD library to create a transparent image.
  380.  */
  381. function zen_create_gd_image($width, $height, $background = 'transparent') {
  382.         $image = imagecreatetruecolor($width, $height);
  383.  
  384.         // default to white as "background" -> better rendering on bright pages
  385.         // when downsampling to gif with just boolean transparency
  386.         $transparent = !(strpos($background, 'transparent') === FALSE);
  387.         $background = trim(ereg_replace('transparent', '', $background));
  388.         list($red, $green, $blue)= split('[, :]', $background);
  389.         if (preg_match('/[0-9]+/', $red.$green.$blue)) {
  390.                 $background_color = imagecolorallocate($image, intval($red), intval($green), intval($blue));
  391.         } else {
  392.                 $background_color = imagecolorallocate($image, 255, 255, 255);
  393.         }
  394.         imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $background_color);
  395.         if ($transparent) {
  396.                 imagecolortransparent($image, $background_color);
  397.         }
  398.         return $image;
  399. }
  400.  
  401. /**
  402.  * Use GD library to overlay a source image with a watermark with given
  403.  * opacity and save the composition to the filesystem.
  404.  */
  405. function zen_gd_watermark_image($source_name, $watermark_name, $destination_name, $quality = 75) {
  406.         bmz_set_visible($source_name);
  407.         $image = zen_load_gd_image($source_name);
  408.         imagealphablending($image, true);
  409.         $watermark = zen_load_gd_image($watermark_name);
  410.         $imagewidth = imagesx($image);
  411.         $imageheight = imagesy($image);
  412.         $watermarkwidth =  imagesx($watermark);
  413.         $watermarkheight =  imagesy($watermark);
  414.  
  415.         // Calculate watermark position from gravity setting. Center as default.
  416.         $startheight = (($imageheight - $watermarkheight)/2);
  417.         $startwidth = (($imagewidth - $watermarkwidth)/2);
  418.         if (!(strpos(WATERMARK_GRAVITY, 'North') === FALSE)) {
  419.                 $startheight = 0;
  420.         } elseif (!(strpos(WATERMARK_GRAVITY, 'South') === FALSE)) {
  421.                 $startheight = $imageheight - $watermarkheight;
  422.         }
  423.         if (!(strpos(WATERMARK_GRAVITY, 'West') === FALSE)) {
  424.                 $startwidth = 0;
  425.         } elseif (!(strpos(WATERMARK_GRAVITY, 'East') === FALSE)) {
  426.                 $startwidth = $imagewidth - $watermarkwidth;
  427.         }
  428.  
  429.         imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
  430.         imagedestroy($watermark);
  431.         zen_save_gd_image($image, $destination_name, $quality);
  432.         imagedestroy($image);
  433.         if (is_file($source_name)) bmz_set_invisible($source_name);
  434. }
  435.  
  436. /**
  437.  * If a combined background (like transparent white or transparent black) is given,
  438.  * we are able to render the semitransparent pixels to what they would look
  439.  * like on the given background-color. The threshold specifies the amount of
  440.  * transparency that is needed, before the pixel is rendered completely transparent.
  441.  * the lower the threshold (0%-100%) is, the more actual color is needed for the
  442.  * semitransparent pixel to be painted.
  443.  */
  444. function zen_imagemagick_render_gif($source_name, $destination_name, $background, $size, $threshold) {
  445.         global $messageStack;
  446.         zen_imagemagick_setenv();
  447.         bmz_set_visible($source_name);
  448.         bmz_set_visible($destination_name);
  449.         $image_extension = substr($source_name, strrpos($source_name, '.'));
  450.         // get image base by stripping off image extension
  451.         $source_base = ereg_replace($image_extension . '$', '', $source_name);
  452.     $background_image = $source_base . "-BACKGROUND.png";
  453.     // create background colored image with dimensions from source image
  454.     $environment = zen_imagemagick_getenv() . ' '; 
  455.         $command = DIR_FS_IMAGE_MAGICK . "convert -size " . $size . " " . $source_name . " -fill " . $background . " -draw 'color 0,0 reset' -geometry " . $size . " " . $background_image;
  456.         exec($command . ' 2>&1', $message);
  457.         if ($message) zen_add_message(sprintf(ERROR_EXECUTING_IMAGE_MAGICK, $command) . implode('<br/>', $message), 'error');
  458.         $message = null;
  459.  
  460.         // cut out shape from background image and scale to specified .
  461.         $command = DIR_FS_IMAGE_MAGICK . "convert " . $background_image . " -size " . $size . " " . $source_name . " -channel Alpha -threshold " . $threshold . " -compose CopyOpacity -geometry " . $size . " -composite " . $background_image;
  462.         exec($command . ' 2>&1', $message);
  463.         if ($message) zen_add_message(sprintf(ERROR_EXECUTING_IMAGE_MAGICK, $command) . implode('<br/>', $message), 'error');
  464.         $message = null;
  465.         //overlay image onto background
  466.         $command = DIR_FS_IMAGE_MAGICK . "composite -compose Atop -size " . $size . " " . $source_name . " " . $background_image . " -geometry " . $size . " " . $destination_name;
  467.         exec($command . ' 2>&1', $message);
  468.         if ($message) zen_add_message(sprintf(ERROR_EXECUTING_IMAGE_MAGICK, $command) . implode('<br/>', $message), 'error');
  469.         $message = null;
  470.         // resize image canvas to specified size
  471.         $command = DIR_FS_IMAGE_MAGICK . "montage -background transparent -size " . $size . " " . $destination_name . " -geometry " . $size . " " . $destination_name;
  472.         exec($command . ' 2>&1', $message);
  473.         if ($message) zen_add_message(sprintf(ERROR_EXECUTING_IMAGE_MAGICK, $command) . implode('<br/>', $message), 'error');
  474.         $message = null;
  475.         bmz_set_visible($destination_name);
  476.         // remove background image
  477.     if (is_file($background_image)) {
  478.             @unlink($background_image);
  479.         }
  480. }
  481.  
  482. /**
  483.  * Use ImageMagic binaries to ovarlay a source image with a watermark
  484.  * and save the composition to the filesystem.
  485.  */
  486. function zen_imagemagick_watermark_image($source_name, $watermark_name, $destination_name, $background, $quality='75') {
  487.         zen_imagemagick_setenv();
  488.         bmz_set_visible($source_name);
  489.         bmz_set_visible($destination_name);
  490.         list($width, $height) = getimagesize($source_name);
  491.         $size = $width . 'x' . $height;
  492.         $image_extension = substr($destination_name, strrpos($destination_name, '.'));
  493.         $command = DIR_FS_IMAGE_MAGICK . 'convert -size ' . $size . ' ' . $source_name . ' -geometry ' . $size . ' -gravity ' . WATERMARK_GRAVITY . ' ' . $watermark_name . ' -compose Over -geometry 100% -composite ';
  494.     $environment = zen_imagemagick_getenv() . ' '; 
  495.         // if $background string contains no 'transparent' substring or image is no gif
  496.         if (($image_extension != '.gif') || (strpos($background, 'transparent') === FALSE)) {
  497.                         $command .= $destination_name;
  498.                 exec($command . ' 2>&1', $message);
  499.                 if ($message) zen_add_message(sprintf(ERROR_EXECUTING_IMAGE_MAGICK, $command) . implode('<br/>', $message), 'error');
  500.                 $message = null;
  501.         } else {
  502.                 // special treatment for smooth transparent gif to background color rendering
  503.                 $background = trim(ereg_replace('transparent', '', $background));
  504.                 $image_base = ereg_replace($image_extension . '$', '', $destination_name);
  505.                 $temp_image = $image_base . '.png';
  506.                 $command .= $temp_image;
  507.                 exec($command . ' 2>&1', $message);
  508.                 if ($message) zen_add_message(sprintf(ERROR_EXECUTING_IMAGE_MAGICK, $command) . implode('<br/>', $message), 'error');
  509.                 $message = null;
  510.                 $background = ($background != '') ? $background : 'white';
  511.                 $threshold = "90%"; // threshold where semitransparent pixels turn to background color.
  512.                 zen_imagemagick_render_gif($temp_image, $destination_name, $background, $size, $threshold);
  513.                 if (($destination_name != $temp_image) && (is_file($temp_image))){
  514.                         @unlink($temp_image);
  515.                 }
  516.         }
  517.         if (is_file($source_name)) bmz_set_invisible($source_name);
  518.         bmz_set_visible($destination_name);
  519. }
  520.  
  521. /**
  522.  * create a new watermarked image composing the original image specified by
  523.  * image_path, image_base and image_extension and the watermark itself
  524.  */
  525. function zen_create_marked_small_image($image_name) {
  526.         $watermark_name = DIR_WS_IMAGES . 'watermark.png';
  527.         $unmarked_image_name = DIR_WS_IMAGES . $image_name;
  528.         $watermarked_image_name = DIR_WS_IMAGES . 'watermark/' . $image_name;
  529.         $image_path = dirname($image_name);
  530.         mkdirr(DIR_FS_CATALOG . DIR_WS_IMAGES . "watermark/" . $image_path);
  531.         if (is_file(DIR_FS_CATALOG . $watermark_name)) {
  532.                 if (IMAGE_MANAGER_HANDLER == 'ImageMagick') {
  533.                         zen_imagemagick_watermark_image(DIR_FS_CATALOG . $unmarked_image_name, DIR_FS_CATALOG . $watermark_name, DIR_FS_CATALOG . $watermarked_image_name, SMALL_IMAGE_BACKGROUND, SMALL_IMAGE_QUALITY);
  534.                 } elseif (IMAGE_MANAGER_HANDLER == 'GD') {
  535.                         zen_gd_watermark_image(DIR_FS_CATALOG . $unmarked_image_name, DIR_FS_CATALOG . $watermark_name, DIR_FS_CATALOG . $watermarked_image_name, SMALL_IMAGE_QUALITY);
  536.                 }
  537.                 if (is_file(DIR_FS_CATALOG . $unmarked_image_name)) bmz_set_invisible(DIR_FS_CATALOG . $unmarked_image_name);
  538.         }
  539. }
  540.  
  541. /**
  542.  * create a new watermarked image composing the original image specified by
  543.  * image_path, image_base and image_extension and the watermark itself
  544.  */
  545. function zen_create_marked_medium_image($image_base, $image_extension) {
  546.         $watermark_name = DIR_WS_IMAGES . 'medium/watermark' . IMAGE_SUFFIX_MEDIUM . '.png';
  547.         $unmarked_image_name = DIR_WS_IMAGES . 'medium/' . $image_base . IMAGE_SUFFIX_MEDIUM . $image_extension;
  548.         $watermarked_image_name = DIR_WS_IMAGES . 'medium/watermark/' . $image_base . IMAGE_SUFFIX_MEDIUM . $image_extension;
  549.         $image_path = dirname($image_base);
  550.         mkdirr(DIR_FS_CATALOG . DIR_WS_IMAGES . "medium/watermark/" . $image_path);
  551.         if (is_file(DIR_FS_CATALOG . $watermark_name)) {
  552.                 if (IMAGE_MANAGER_HANDLER == 'ImageMagick') {
  553.                         zen_imagemagick_watermark_image(DIR_FS_CATALOG . $unmarked_image_name, DIR_FS_CATALOG . $watermark_name, DIR_FS_CATALOG . $watermarked_image_name, MEDIUM_IMAGE_BACKGROUND, MEDIUM_IMAGE_QUALITY);
  554.                 } elseif (IMAGE_MANAGER_HANDLER == 'GD') {
  555.                         zen_gd_watermark_image(DIR_FS_CATALOG . $unmarked_image_name, DIR_FS_CATALOG . $watermark_name, DIR_FS_CATALOG . $watermarked_image_name, MEDIUM_IMAGE_QUALITY);
  556.                 }
  557.                 if (is_file(DIR_FS_CATALOG . $unmarked_image_name)) bmz_set_invisible(DIR_FS_CATALOG . $unmarked_image_name);
  558.         }
  559. }
  560.  
  561. /**
  562.  * create a new watermarked image composing the original image specified by
  563.  * image_path, image_base and image_extension and the watermark itself
  564.  */
  565. function zen_create_marked_large_image($image_base, $image_extension) {
  566.         $watermark_name = DIR_WS_IMAGES . 'large/watermark' . IMAGE_SUFFIX_LARGE . '.png';
  567.         $unmarked_image_name = DIR_WS_IMAGES . 'large/' . $image_base . IMAGE_SUFFIX_LARGE . $image_extension;
  568.         $watermarked_image_name = DIR_WS_IMAGES . 'large/watermark/' . $image_base . IMAGE_SUFFIX_LARGE . $image_extension;
  569.         $image_path = dirname($image_base);
  570.         mkdirr(DIR_FS_CATALOG . DIR_WS_IMAGES . "large/watermark/" . $image_path);
  571.         if (is_file(DIR_FS_CATALOG . $watermark_name)) {
  572.                 if (IMAGE_MANAGER_HANDLER == 'ImageMagick') {
  573.                         zen_imagemagick_watermark_image(DIR_FS_CATALOG . $unmarked_image_name, DIR_FS_CATALOG . $watermark_name, DIR_FS_CATALOG . $watermarked_image_name, LARGE_IMAGE_BACKGROUND, LARGE_IMAGE_QUALITY);
  574.                 } elseif (IMAGE_MANAGER_HANDLER == 'GD') {
  575.                         zen_gd_watermark_image(DIR_FS_CATALOG . $unmarked_image_name, DIR_FS_CATALOG . $watermark_name, DIR_FS_CATALOG . $watermarked_image_name, LARGE_IMAGE_QUALITY);
  576.                 }
  577.                 if (is_file(DIR_FS_CATALOG . $unmarked_image_name)) bmz_set_invisible(DIR_FS_CATALOG . $unmarked_image_name);
  578.         }
  579. }
  580.  
  581.  
  582. function zen_add_message($message, $message_type) {
  583.         global $messageStack;
  584. //      if ($this->message_location == 'direct') {
  585.                 $messageStack->add($message, $message_type);
  586. //      } else {
  587. //              $messageStack->add_session($message, $message_type);
  588. //      }
  589. }
  590.  
  591. function bmz_set_visible($filename) {
  592.         // permissions are taken from images folder. if you want to override:
  593.         // set $perms to 0777 or 0666 if your scripts are running as e.g. 'apache', 'www' or 'nobody' instead of your own username.
  594.         // set $perms to 0755 or 0644 if your scripts are running as your own user instead of e.g. 'nobody'.
  595.         $perms = fileperms(DIR_FS_CATALOG . DIR_WS_IMAGES);
  596.         $perms &= 00777; //remove sticky bits
  597.         //Now transform x to r for files
  598.         $perms |= (($perms & 00001) == 00001) ? 00004 : 00000;
  599.         $perms |= (($perms & 00010) == 00010) ? 00040 : 00000;
  600.         $perms |= (($perms & 00100) == 00100) ? 00400 : 00000;
  601.  
  602.         @chmod($filename, $perms);
  603.         if (CLONE_IMAGE_DIR_OWNER == 'true') {
  604.                 $owner = fileowner(DIR_FS_CATALOG . DIR_WS_IMAGES);
  605. //              echo 'Change owner of ' . $filename . ' to ' . $owner . ': ';
  606.                 @chown($filename, $owner);
  607.         }
  608. }
  609.  
  610. function bmz_set_invisible($filename) {
  611.         $perms = 0000;
  612.         @chmod($filename, $perms);
  613. }
  614.  
  615.         /**
  616.          * Create a directory structure recursively
  617.          *
  618.          * @author      Aidan Lister <aidan@php.net>
  619.          * @version     1.0.0
  620.          * @param       string   $pathname    The directory structure to create
  621.          * @return      bool     Returns TRUE on success, FALSE on failure
  622.          */
  623.         // permissions are taken from images folder. if you want to override:
  624.         // set $mode to 0777 if your scripts are run as e.g. 'apache', 'www' or 'nobody' instead of your own username.
  625.         // set $mode to 0755 or 0711 if your scripts are run as your own user instead of e.g. 'nobody'.
  626.         function mkdirr($pathname, $mode = null, $owner = null)
  627.         {
  628.                 if ($mode == null) {
  629.                         $mode = fileperms(DIR_FS_CATALOG . DIR_WS_IMAGES);
  630.                         $mode &= 00777; //remove sticky bits
  631.  
  632.                 }
  633.                 if (($owner == null) && (CLONE_IMAGE_DIR_OWNER == 'true')) {
  634.                         $owner = fileowner(DIR_FS_CATALOG . DIR_WS_IMAGES);
  635.                 }
  636.  
  637.             // Check if directory already exists
  638.             if (is_dir($pathname) || empty($pathname)) {
  639.                 return true;
  640.             }
  641.  
  642.             // Ensure a file does not already exist with the same name
  643.             if (is_file($pathname)) {
  644.                 trigger_error('mkdirr() File exists', E_USER_WARNING);
  645.                 return false;
  646.             }
  647.  
  648.             // Crawl up the directory tree
  649.             $next_pathname = substr($pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR));
  650.             if (mkdirr($next_pathname, $mode, $owner)) {
  651.                 if (!file_exists($pathname)) {
  652.                     $error = mkdir($pathname, $mode);
  653.                                 if (CLONE_IMAGE_DIR_OWNER == 'true') {
  654.                                         @chown($pathname, $owner);
  655.                                 }
  656.                                 return $error;
  657.                 }
  658.             }
  659.  
  660.             return false;
  661.         }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right