--- imagecache0.module Sun Jun 17 01:22:42 2007 +++ imagecache.module Thu Oct 18 18:39:28 2007 @@ -140,7 +140,11 @@ function imagecache_cache() { // Prepend presetname to tmp file name to prevent namespace clashes when // multiple presets for the same image are called in rapid succession. $tmpdestination = file_directory_temp() .'/'. $preset . str_replace(dirname($path) .'/', '', $path); + $tmpdestination2 = file_directory_temp() .'/'. 'temp' . $preset . str_replace(dirname($path) .'/', '', $path); +// watchdog('imagecache', t('Path= %id .', array('%id' => $path)), WATCHDOG_ERROR); + + $dir = dirname($destination); // Build the destination folder tree if it doesn't already exists. @@ -177,6 +181,23 @@ function imagecache_cache() { $action['data'][$key] = _imagecache_filter($key, $value, $size[0], $size[1], $new_width, $new_height); } switch ($action['data']['function']) { +// case 'getsize': +// $size = getimagesize($tmpdestination); +// $new_width = _imagecache_filter('width', $action['data']['width'], $size[0], $size[1]); +// $new_height = _imagecache_filter('height', $action['data']['height'], $size[0], $size[1]); +// if (!image_resize($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) { +// watchdog('imagecache', t('Imagecache resize action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); +// } +// break; + case 'templayer': + if (!image_create($tmpdestination, $tmpdestination2, $action['data']['width'], $action['data']['height'], $action['data']['R'], $action['data']['G'], $action['data']['B'])) { + watchdog('imagecache', t('Imagecache templayer action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); + }else{ +// $t = $tmpdestination; +// $tmpdestination = $tmpdestination2; +// $tmpdestination2 = $t; + } + break; case 'resize': if (!image_resize($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) { watchdog('imagecache', t('Imagecache resize action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); @@ -201,9 +222,39 @@ function imagecache_cache() { watchdog('imagecache', t('Imagecache crop action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); } break; + case 'image2templayer': + if (!image_combine($tmpdestination, $tmpdestination2, $action['data']['xoffset2'], $action['data']['yoffset2'], $action['data']['alpha'])) { + watchdog('imagecache', t('Imagecache image2templayer action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); + } + break; + case 'file2templayer': + $path_file = file_create_path() .'/' . $action['data']['path']; + // Verify that the source exists, if not exit. Maybe display a missing image. + $source_file = file_create_path($path_file); + if (!is_file($source_file)) { + return drupal_not_found(); + // @todo: Add a global not found image, and a not found image per preset, + // and deliver here if the preset is flagged to provide a not found image. + } + if (!image_combine($source_file, $tmpdestination2, $action['data']['xoffset2'], $action['data']['yoffset2'], $action['data']['alpha'])) { + watchdog('imagecache', t('Imagecache image2templayer action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); + } + break; + + case 'swap_image_and_templayer': +// echo"
"; +// print_r($action); +// echo""; +// watchdog('imagecache', print_r($action).t('Imagecache resize action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); + $t = $tmpdestination; + $tmpdestination = $tmpdestination2; + $tmpdestination2 = $t; + break; + } } file_move($tmpdestination, $destination); + file_delete($tmpdestination2); } else { $generated = TRUE; @@ -674,7 +725,85 @@ function _imagecache_actions_form($prese '#description' => t('Enter an offset in pixels or use a keyword: top, center, or bottom.'), ); break; - case 'watermark': + case 'templayer': + $form[$actionid]['data']['width'] = array( + '#type' => 'textfield', + '#title' => t('Width'), + '#default_value' => $action['data']['width'], + '#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'), + ); + $form[$actionid]['data']['height'] = array( + '#type' => 'textfield', + '#title' => t('Height'), + '#default_value' => $action['data']['height'], + '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'), + ); + $form[$actionid]['data']['R'] = array( + '#type' => 'textfield', + '#title' => t('Red'), + '#default_value' => $action['data']['R'], + '#description' => t('Enter a Red 0-255.'), + ); + $form[$actionid]['data']['G'] = array( + '#type' => 'textfield', + '#title' => t('Green'), + '#default_value' => $action['data']['G'], + '#description' => t('Enter a Green 0-255.'), + ); + $form[$actionid]['data']['B'] = array( + '#type' => 'textfield', + '#title' => t('Blue'), + '#default_value' => $action['data']['B'], + '#description' => t('Enter a Blue 0-255.'), + ); + break; + case 'image2templayer': + $form[$actionid]['data']['xoffset2'] = array( + '#type' => 'textfield', + '#title' => t('X offset'), + '#default_value' => $action['data']['xoffset2'], + '#description' => t('Enter an offset in pixels or use a keyword: left, center, or right.'), + ); + $form[$actionid]['data']['yoffset2'] = array( + '#type' => 'textfield', + '#title' => t('Y offset'), + '#default_value' => $action['data']['yoffset2'], + '#description' => t('Enter an offset in pixels or use a keyword: top, center, or bottom.'), + ); + $form[$actionid]['data']['alpha'] = array( + '#type' => 'textfield', + '#title' => t('alpha'), + '#default_value' => $action['data']['alpha'], + '#description' => t('alpha 0-100.'), + ); + break; + case 'file2templayer': + $form[$actionid]['data']['xoffset2'] = array( + '#type' => 'textfield', + '#title' => t('X offset'), + '#default_value' => $action['data']['xoffset2'], + '#description' => t('Enter an offset in pixels or use a keyword: left, center, or right.'), + ); + $form[$actionid]['data']['yoffset2'] = array( + '#type' => 'textfield', + '#title' => t('Y offset'), + '#default_value' => $action['data']['yoffset2'], + '#description' => t('Enter an offset in pixels or use a keyword: top, center, or bottom.'), + ); + $form[$actionid]['data']['alpha'] = array( + '#type' => 'textfield', + '#title' => t('alpha'), + '#default_value' => $action['data']['alpha'], + '#description' => t('alpha 0-100.'), + ); + $form[$actionid]['data']['path'] = array( + '#type' => 'textfield', + '#title' => t('file name'), + '#default_value' => $action['data']['path'], + '#description' => t('file name (file is in the "files" folder)'), + ); + break; + case 'watermark': // Think about this one... } $form[$actionid]['remove'] = array( @@ -691,7 +820,7 @@ function _imagecache_actions_form($prese $form['newaction'] = array( '#type' => 'select', - '#options' => array('' => t('select...'), 'scale' => t('Scale'), 'resize' => t('Resize'), 'crop' => t('Crop')), + '#options' => array('' => t('select...'), 'templayer' => t('Create temp layer'), 'image2templayer' => t('Copy image to temp layer'), 'file2templayer' => t('Copy file to temp layer'), 'swap_image_and_templayer' => t('Swap Temp layer and Image'), 'scale' => t('Scale image'), 'resize' => t('Resize image'), 'crop' => t('Crop image')), '#title' => t('Add a new action'), '#description' => $description, ); @@ -756,3 +885,269 @@ function imagecache_image_flush($path) { file_delete($ipath); } } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +/************************************************************************************************/ +/** + * Create an image to the given dimensions (ignoring aspect ratio). + * + * @param $destination The file path of the destination image. + * @param $width The target width. + * @param $height The target height. + */ +function image_create($source, $destination, $width, $height, $R, $G, $B) { + return image_toolkit_invoke('create', array($source, $destination, $width, $height, $R, $G, $B)); +} + + +/** + * create an image to the specified size using GD. + */ +function image_gd_create($source, $destination, $width, $height, $R, $G, $B) { + if (!file_exists($source)) { + return FALSE; + } + + $info = image_get_info($source); + if (!$info) { + return FALSE; + } + + $im = image_gd_open($source, $info['extension']); + if (!$im) { + return FALSE; + } + + $res = imageCreateTrueColor($width, $height); + if ($info['extension'] == 'png') { + $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); + imagealphablending($res, FALSE); + imagefilledrectangle($res, 0, 0, $width, $height, $transparency); + imagealphablending($res, TRUE); + imagesavealpha($res, TRUE); + }else{ + $color = imagecolorallocate ($res, $R, $G, $B); // что бы при копировании на подложку не вылезал непрозрачный фон, белым цветом ... +// $color = imagecolorallocate ($res, 255, 255, 0); // что бы при копировании на подложку не вылезал непрозрачный фон, белым цветом ... + imagefilledrectangle ($res, 0, 0, $width, $height, $color); // рисуем прямоугольник и ... + imagecolortransparent ($res, $color); // делаем этот цвет прозрачным + } + + //imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']); + $result = image_gd_close($res, $destination, $info['extension']); + + imageDestroy($res); + imageDestroy($im); + + return $result; +} + + +/** + * Crop an image to the rectangle specified by the given rectangle. + * + * @param $source The filepath of the source image + * @param $destination The file path of the destination image + * @param $x The top left co-ordinate of the crop area (x axis value) + * @param $y The top left co-ordinate of the crop area (y axis value) + */ +function image_combine($source, $destination, $x, $y, $alpha) { + return image_toolkit_invoke('combine', array($source, $destination, $x, $y, $alpha)); +} + +/** + * combine an image using the GD toolkit. + */ +function image_gd_combine($source, $destination, $x, $y, $alpha) { + $info_s = image_get_info($source); + if (!$info_s) { + return FALSE; + } + + $info_d = image_get_info($destination); + if (!$info_d) { + return FALSE; + } + $im_s = image_gd_open($source, $info_s['extension']); + $im_d = image_gd_open($destination, $info_d['extension']); + $res = imageCreateTrueColor( $info_d['width'], $info_d['height']); + + $white_color = imagecolorallocate ($res, 255, 255, 255); // что бы при копировании на подложку не вылезал непрозрачный фон, белым цветом ... + imagefilledrectangle ($res, 0, 0, $info_d['width'], $info_d['height'], $white_color); // рисуем прямоугольник и ... + imagecolortransparent ($res, $white_color); // делаем этот цвет прозрачным +// imageCopy($res, $im, 0, 0, $x, $y, $width, $height); +// imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']); + +// считаем координаты для вставки вод. знака +$x_ins = $y_ins = 0; +if(is_numeric($x)){ + $x_ins = $x; +}else{switch ( $x ){ + case 'center' : + $x_ins = floor( ( $info_d['width'] - $info_s['width']) / 2 ); + break; + case 'right' : + $x_ins = $info_d['width'] - $info_s['width']; + break; + default: + $x_ins = 0; + break; + +} +} +if(is_numeric($y)){ + $y_ins = $y; +}else{ +switch ( $y ){ + case 'center' : + $y_ins = floor( ( $info_d['height'] - $info_s['height']) / 2 ); + break; + case 'bottom' : + $y_ins = $info_d['height'] - $info_s['height']; + break; + default: + $y_ins = 0; + break; +} +} +// ну и кладем на подложку вод.знак в вычисленные координаты +imagecopy($res, $im_d, 0, 0, 0, 0, $info_d['width'], $info_d['height']); + +if($info_s['extension']=='png'){ + $watermark = new watermark(); +// $main_img_obj = imagecreatefromjpeg($_GET['main']); + $watermark_img_obj = imagecreatefrompng($source); + $res = $watermark->create_watermark($res, $watermark_img_obj, $x_ins, $y_ins, $alpha); + +} +else{ +imagecopymerge($res, $im_s, $x_ins, $y_ins, 0, 0, $info_s['width'], $info_s['height'], $alpha); +//imagecopymerge($res, $im_s, 0, 0, 0, 0, $info_s['width'], $info_s['height'], $alpha); +} + $result = image_gd_close($res, $destination, $info_d['extension']); + + imageDestroy($res); + imageDestroy($im_s); + imageDestroy($im_d); + + return $result; +} + + + + + + + + + + + + + + + + + + + + +/****************************************************************************************************************************************/ +//Автор: Niko (http://www.codeguru.com.ua) +class watermark{ + function create_watermark( $main_img_obj, $watermark_img_obj, $x_ins, $y_ins, $alpha_level = 100 ) { + $alpha_level /= 100; + + + $main_img_obj_w = imagesx( $main_img_obj ); + $main_img_obj_h = imagesy( $main_img_obj ); + $watermark_img_obj_w = imagesx( $watermark_img_obj ); + $watermark_img_obj_h = imagesy( $watermark_img_obj ); + + + $main_img_obj_min_x = $x_ins; + $main_img_obj_min_y = $y_ins; + +// $main_img_obj_min_x = floor( ( $main_img_obj_w / 2 ) - ( $watermark_img_obj_w / 2 ) ); +// $main_img_obj_max_x = ceil( ( $main_img_obj_w / 2 ) + ( $watermark_img_obj_w / 2 ) ); +// $main_img_obj_min_y = floor( ( $main_img_obj_h / 2 ) - ( $watermark_img_obj_h / 2 ) ); +// $main_img_obj_max_y = ceil( ( $main_img_obj_h / 2 ) + ( $watermark_img_obj_h / 2 ) ); + + + $return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h ); + + + for( $y = 0; $y < $main_img_obj_h; $y++ ) { + for( $x = 0; $x < $main_img_obj_w; $x++ ) { + $return_color = NULL; + + + $watermark_x = $x - $main_img_obj_min_x; + $watermark_y = $y - $main_img_obj_min_y; + + + $main_rgb = imagecolorsforindex( $main_img_obj, imagecolorat( $main_img_obj, $x, $y ) ); + + + if ( $watermark_x >= 0 && $watermark_x < $watermark_img_obj_w && + $watermark_y >= 0 && $watermark_y < $watermark_img_obj_h ) { + $watermark_rbg = imagecolorsforindex( $watermark_img_obj, imagecolorat( $watermark_img_obj, $watermark_x, $watermark_y ) ); + + + $watermark_alpha = round( ( ( 127 - $watermark_rbg['alpha'] ) / 127 ), 2 ); + $watermark_alpha = $watermark_alpha * $alpha_level; + + $avg_red = $this->_get_ave_color( $main_rgb['red'], $watermark_rbg['red'], $watermark_alpha ); + $avg_green = $this->_get_ave_color( $main_rgb['green'], $watermark_rbg['green'], $watermark_alpha ); + $avg_blue = $this->_get_ave_color( $main_rgb['blue'], $watermark_rbg['blue'], $watermark_alpha ); + + $return_color = $this->_get_image_color( $return_img, $avg_red, $avg_green, $avg_blue ); + } else { + $return_color = imagecolorat( $main_img_obj, $x, $y ); + } + imagesetpixel( $return_img, $x, $y, $return_color ); + } + } + return $return_img; + } + + function _get_ave_color( $color_a, $color_b, $alpha_level ) { + return round( ( ( $color_a * ( 1 - $alpha_level ) ) + ( $color_b * $alpha_level ) ) ); + } + + function _get_image_color($im, $r, $g, $b) { + $c=imagecolorexact($im, $r, $g, $b); + if ($c!=-1) return $c; + $c=imagecolorallocate($im, $r, $g, $b); + if ($c!=-1) return $c; + return imagecolorclosest($im, $r, $g, $b); + } + +}