Пример #1
0
 /**
  * The main method. This determines the necessary parameters to make or open an image, and interfaces with the CE Image class to make it happen!
  *
  * @return mixed Will return the new tagdata (str) on success or no_results on failure.
  */
 public function make()
 {
     //if the template debugger is enabled, and a super admin user is logged in, enable debug mode
     $debug = FALSE;
     if ($this->EE->session->userdata['group_id'] == 1 && $this->EE->config->item('template_debugging') == 'y') {
         $debug = TRUE;
     }
     //manipulate param
     $manipulate = strtolower($this->EE->TMPL->fetch_param('manipulate')) == 'no' ? FALSE : TRUE;
     //src param
     $src = $this->EE->TMPL->fetch_param('src');
     //get the settings
     $defaults = $manipulate ? $this->get_make_defaults() : $this->get_open_defaults();
     //no need to go forward if there are no src params
     if ($src == '' && $defaults['fallback_src'] == '') {
         if ($debug) {
             $this->EE->TMPL->log_item('  ***  CE Image debug: Source and fallback source cannot both be blank.');
         }
         return $this->EE->TMPL->no_results();
     }
     //check for file paths and replace them if needed
     $src = $this->parse_ee_paths($src);
     $defaults['fallback_src'] = $this->parse_ee_paths($defaults['fallback_src']);
     //include CE Image
     if (!class_exists('Ce_image')) {
         require PATH_THIRD . 'ce_img/libraries/Ce_image.php';
     }
     //initialize with the settings
     $image = new Ce_image($defaults);
     unset($defaults);
     if ($manipulate) {
         if (!$image->make($src)) {
             //there was a problem
             if ($debug) {
                 $this->log_debug_messages($image->get_debug_messages());
             }
             $image->close();
             return $this->EE->TMPL->no_results();
         }
     } else {
         if (!$image->open($src)) {
             //there was a problem
             if ($debug) {
                 $this->log_debug_messages($image->get_debug_messages());
             }
             $image->close();
             return $this->EE->TMPL->no_results();
         }
     }
     //ascii_art param
     $ascii_art = trim($this->EE->TMPL->fetch_param('ascii_art'));
     if ($ascii_art != '') {
         $temp = explode('|', $ascii_art);
         $ascii_art = array();
         if (is_array($temp)) {
             $t = trim(strtolower($temp[0]));
             if ($t == 'yes' || $t == 'y' || $t == 'on') {
                 $ascii_art[0] = TRUE;
             } else {
                 $ascii_art[0] = FALSE;
             }
             $ascii_art[1] = array('#', '@', '%', '=', '+', '*', ':', '-', '.', ' ');
             if (isset($temp[1])) {
                 $arr = explode(',', $temp[1]);
                 if (count($arr) > 0) {
                     $ascii_art[1] = $arr;
                 }
                 unset($arr);
             }
             $ascii_art[2] = FALSE;
             if (isset($temp[2])) {
                 $ascii_art[2] = $temp[2] == 'yes' || $temp[2] == 'y' || $temp[2] == 'on';
             }
             $ascii_art[3] = FALSE;
             if (isset($temp[3])) {
                 $ascii_art[3] = $temp[3] == 'yes' || $temp[3] == 'y' || $temp[3] == 'on';
             }
         } else {
             $t = trim(strtolower($temp));
             $ascii_art[0] = $t == 'yes' || $t == 'y' || $t == 'on';
         }
         $ascii_art = call_user_func_array(array($image, 'get_ascii_art'), $ascii_art);
     }
     //top_colors param
     $top_colors = $this->EE->TMPL->fetch_param('top_colors');
     $top_colors = $top_colors != '' ? explode('|', $top_colors) : '';
     if ($top_colors != '') {
         $tc_count = count($top_colors);
         if ($tc_count > 1) {
             $top_colors = $image->get_top_colors($top_colors[0], $top_colors[1]);
         } else {
             if ($tc_count > 0) {
                 $top_colors = $image->get_top_colors($top_colors[0]);
             } else {
                 $top_colors = '';
             }
         }
     }
     //get the var prefix
     $var_prefix = '';
     $tag_parts = $this->EE->TMPL->tagparts;
     if (is_array($tag_parts) && isset($tag_parts[2])) {
         $var_prefix = $tag_parts[2] . ':';
     }
     //get return tag param
     $create_tag = strtolower($this->EE->TMPL->fetch_param('create_tag')) == 'yes';
     if ($this->EE->TMPL->fetch_param('url_only') == 'yes') {
         $tagdata = '{' . $var_prefix . 'made}';
         $create_tag = FALSE;
     } else {
         if ($this->EE->TMPL->fetch_param('output') != '') {
             $tagdata = $this->EE->TMPL->fetch_param('output');
             $create_tag = FALSE;
         } else {
             //tagdata
             $tagdata = $this->EE->TMPL->tagdata;
             if ($create_tag) {
                 if (strpos($tagdata, '<img ') !== FALSE) {
                     //the image has an image tag inside, so ignore request to return a tag
                     $create_tag = FALSE;
                 }
             } else {
                 if ($this->EE->TMPL->fetch_param('create_tag') == '' && $tagdata == '') {
                     $create_tag = TRUE;
                 }
             }
             //create the tag
             if ($create_tag) {
                 //get the settings as to whether or not to automatically add the dimensions to the tag
                 $add_dimensions = $this->ee_string_to_bool($this->determine_setting('add_dimensions', 'y'));
                 if (trim($tagdata) == '') {
                     //get the tag params
                     $tag_params = $this->EE->TMPL->tagparams;
                     //determine the attributes by getting rid of the parameters
                     foreach ($tag_params as $param => $value) {
                         if (in_array($param, self::$valid_params)) {
                             unset($tag_params[$param]);
                         }
                     }
                     if ($add_dimensions) {
                         $tag_params['width'] = '{width}';
                         $tag_params['height'] = '{height}';
                     }
                     if (!isset($tag_params['alt'])) {
                         $tag_params['alt'] = '';
                     }
                     //add in the src attribute
                     $tag_params = array_merge(array('src' => '{made}'), $tag_params);
                     //get the attributes parameter
                     $attributes = $this->EE->TMPL->fetch_param('attributes');
                     //turn the attributes into key => value pairs
                     preg_match_all('@(\\S+?)\\s*=\\s*(\\042|\\047)([^\\2]*?)\\2@is', $attributes, $attributes, PREG_SET_ORDER);
                     //this will hold the image attribute pairs
                     $pairs = array();
                     foreach ($attributes as $attribute) {
                         //attribute => value
                         $pairs[$attribute[1]] = $attribute[3];
                     }
                     //mess with styles if applicable
                     if (!empty($pairs['style'])) {
                         //the final css array
                         $css = array();
                         //get the original (tag param) style attribute
                         $styles = isset($tag_params['style']) ? $tag_params['style'] : FALSE;
                         //unset the attribute
                         unset($tag_params['style']);
                         //loop through the styles and add them to the css array
                         if (!empty($styles)) {
                             $style_array = explode(';', $styles);
                             //get the rules
                             foreach ($style_array as $rule) {
                                 $rule = trim($rule);
                                 if (empty($rule)) {
                                     continue;
                                 }
                                 $rule = explode(':', $rule, 2);
                                 //split into prop, value pairs
                                 $css[strtolower($rule[0])] = trim($rule[1]);
                             }
                             unset($style_array);
                         }
                         //loop through the styles passed into the attributes array
                         $style_array = explode(';', $pairs['style']);
                         //get the rules
                         if (count($style_array) > 0) {
                             foreach ($style_array as $rule) {
                                 $rule = trim($rule);
                                 if (empty($rule)) {
                                     continue;
                                 }
                                 $rule = explode(':', $rule, 2);
                                 //split into prop, value pairs
                                 $property = strtolower($rule[0]);
                                 $value = trim($rule[1]);
                                 //if the value is blank, the user is opting to remove it
                                 if ($value === '') {
                                     //unset the property
                                     unset($css[$property]);
                                     $length = strlen($property) + 1;
                                     //also remove any descendant rules like 'margin-left' if 'margin' is being removed
                                     foreach ($css as $p => $v) {
                                         if (substr($p, 0, $length) == $property . '-') {
                                             //unset the property
                                             unset($css[$p]);
                                         }
                                     }
                                 } else {
                                     $css[$property] = $value;
                                 }
                             }
                         }
                         //rebuild the styles
                         $pairs['style'] = '';
                         foreach ($css as $key => $value) {
                             $pairs['style'] .= $key . ': ' . $value . '; ';
                         }
                         $pairs['style'] = trim($pairs['style']);
                     }
                     //combine with the parameters - the pairs will have precedence of any conflicts
                     $attributes = array_merge($tag_params, $pairs);
                     //if the user set the width or height to '', remove the attribute
                     if (isset($attributes['width']) && $attributes['width'] == '') {
                         unset($attributes['width']);
                     }
                     if (isset($attributes['height']) && $attributes['height'] == '') {
                         unset($attributes['height']);
                     }
                     //create the tag
                     $tagdata = '<img ';
                     foreach ($attributes as $param => $value) {
                         $tagdata .= strpos($value, '"') === FALSE ? $param . '="' . $value . '" ' : $param . "='" . $value . "' ";
                     }
                     $tagdata .= '/>';
                     unset($pairs, $tag_params, $attributes);
                 }
             }
         }
     }
     //encode the URLs?
     $encode_urls = $this->ee_string_to_bool($this->determine_setting('encode_urls', 'yes'));
     $defaults['encode_urls'] = $encode_urls;
     //---------- return data ----------
     //current data
     $relative = $encode_urls ? $this->url_encode_lite($image->get_relative_path()) : $image->get_relative_path();
     $filename = $image->get_filename();
     $absolute = $image->get_server_path();
     $width = $image->get_width();
     $height = $image->get_height();
     $extension = $image->get_extension();
     $filesize = strpos($tagdata, '{' . $var_prefix . 'filesize}') !== FALSE ? $image->get_filesize() : '';
     $filesize_bytes = strpos($tagdata, '{' . $var_prefix . 'filesize_bytes}') !== FALSE ? $image->get_filesize(TRUE) : '';
     $type = $image->get_type();
     $base64 = strpos($tagdata, '{' . $var_prefix . 'base64}') !== FALSE ? $this->base64_image($absolute, $type) : '';
     //original data
     $relative_orig = $encode_urls ? $this->url_encode_lite($image->get_original_relative_path()) : $image->get_original_relative_path();
     $filename_orig = $image->get_original_filename();
     $absolute_orig = $image->get_original_server_path();
     $width_orig = $image->get_original_width();
     $height_orig = $image->get_original_height();
     $extension_orig = $image->get_original_extension();
     $filesize_orig = strpos($tagdata, '{' . $var_prefix . 'filesize_orig}') !== FALSE ? $image->get_original_filesize() : '';
     $filesize_bytes_orig = strpos($tagdata, '{' . $var_prefix . 'filesize_bytes_orig}') !== FALSE ? $image->get_original_filesize(TRUE) : '';
     $type_orig = $image->get_type_orig();
     $base64_orig = strpos($tagdata, '{' . $var_prefix . 'base64_orig}') !== FALSE ? $this->base64_image($absolute_orig, $type_orig) : '';
     $average_color = strpos($tagdata, '{' . $var_prefix . 'average_color}') !== FALSE ? $image->get_average_color() : '';
     //for extension devs - returns 'saved', 'cached', or 'none' depending on what was done with the image
     $final_action = $image->get_final_action();
     //log the debug array
     if ($debug) {
         $this->log_debug_messages($image->get_debug_messages());
     }
     //close the image
     $image->close();
     unset($image);
     //conditionals
     $conditionals = array();
     $conditionals['top_colors'] = is_array($top_colors) ? 'TRUE' : 'FALSE';
     $conditionals['ascii_art'] = $ascii_art == '' ? 'FALSE' : 'TRUE';
     $tagdata = $this->EE->functions->prep_conditionals($tagdata, $conditionals);
     $variables = array($var_prefix . 'sized' => $relative, $var_prefix . 'made' => $relative, $var_prefix . 'orig' => $relative_orig, $var_prefix . 'made_url' => $this->EE->functions->create_url($relative), $var_prefix . 'orig_url' => $this->EE->functions->create_url($relative_orig), $var_prefix . 'width' => $width, $var_prefix . 'width_orig' => $width_orig, $var_prefix . 'height' => $height, $var_prefix . 'height_orig' => $height_orig, $var_prefix . 'type' => $type, $var_prefix . 'type_orig' => $type_orig, $var_prefix . 'w' => $width, $var_prefix . 'h' => $height, $var_prefix . 'name' => $filename, $var_prefix . 'name_orig' => $filename_orig, $var_prefix . 'path' => $absolute, $var_prefix . 'path_orig' => $absolute_orig, $var_prefix . 'extension' => $extension, $var_prefix . 'extension_orig' => $extension_orig, $var_prefix . 'filesize' => $filesize, $var_prefix . 'filesize_bytes' => $filesize_bytes, $var_prefix . 'filesize_orig' => $filesize_orig, $var_prefix . 'filesize_bytes_orig' => $filesize_bytes_orig, $var_prefix . 'base64' => $base64, $var_prefix . 'base64_orig' => $base64_orig, $var_prefix . 'ascii_art' => $ascii_art, $var_prefix . 'average_color' => $average_color, $var_prefix . 'top_colors' => $top_colors, $var_prefix . 'final_action' => $final_action);
     //pre parse hook
     if ($this->EE->extensions->active_hook('ce_img_pre_parse')) {
         $tagdata = $this->EE->extensions->call('ce_img_pre_parse', $tagdata, $variables, $var_prefix);
     }
     //parse
     $parsed = $this->EE->TMPL->parse_variables_row($tagdata, $variables);
     //free up some memory
     unset($ascii_art, $top_colors, $base64, $base64_orig, $variables, $tagdata);
     return $parsed;
 }