function smarty_function_picture($params, $template) { $src = smarty_plugin_get_variable($params, $template, 'src', true); $alt = smarty_plugin_get_variable($params, $template, 'alt'); $path = smarty_plugin_get_variable($params, $template, 'path'); $CI =& get_instance(); $CI->load->helper('image'); $file = find_img($src); ci_log('The image file that needs to be picture is %s', $file); if ($file == null) { // We can't read the file $src = 'default.png'; } $size = get_image_size($src); $size = $size['width']; $attr = $params; $medias = array(); $ret = array(); $attr['src'] = $src; if ($path != '') { $attr['path'] = site_url($path); } $ret[] = '<picture ' . _parse_form_attributes($attr, array()) . ' >'; foreach ($params as $key => $value) { // Check if user has set the customized media if (strpos($key, 'media') !== false) { $media = str_replace('media', '', $key); $medias[] = $media; $ret[] = "\t" . '<source src="' . site_url('responsive/size/' . $value . '/' . $src) . '" media="(min-width:' . $media . 'px)">'; continue; } $attr[$key] = smarty_plugin_get_variable($params, $template, $key, false); } if ($path == '') { $resolutions = get_ci_config('resolutions'); foreach ($resolutions as $res) { if (array_search($res, $medias) !== false) { // If the resolution is already covered continue; } $ret[] = "\t" . '<source src="' . site_url('responsive/size/' . (double) $res / 2880 * (double) $size . '/' . $src) . '" media="(min-width:' . $res . 'px)">'; } } $ret[] = "\t" . '<noscript>'; $ret[] = "\t\t" . '<img src="' . site_url('static/img/' . $src) . '" alt="' . $alt . '">'; $ret[] = "\t" . '</noscript>'; $ret[] = '</picture>'; return implode("\n", $ret); }
function smarty_function_action($params, $template) { $action = get_default($params, 'obj', null); $alt = get_default($params, 'alt', ''); if ($action == null) { trigger_error('The obj parameter is required for action!'); return ''; } if (gettype($action) != 'object' || get_class($action) != 'Action') { trigger_error('The obj parameter must be of Class Action!'); return ''; } $fields = json_decode($action->fields); ci_log('The action to show is', $action); $data = array(); $uri = $action->uri(); if ($uri) { $data['href'] = $uri; } $content = lang($action->label); if (isset($action->logo) && $action->logo != '') { $data = array('src' => $action->logo, 'path' => '/responsive/size', 'data-placement' => isset($fields->placement) ? $fields->placement : 'right', 'data-original-title' => $action->label); $auto = get_default($params, 'auto', true); if ($auto) { $data["data-toggle"] = "tooltip"; } $CI =& get_instance(); $CI->load->helper('image'); $file = find_img($data['src']); // Try to find the image in static/img if ($file == null) { // We can't read the file $data['src'] = 'default.png'; } $picturedata = $data; unset($picturedata['data-toggle']); $content = smarty_function_picture($picturedata, $template); if (isset($action->controller) && isset($action->method) && $uri) { $data['href'] = $uri; } } return build_tag('a', $data, $content); }
function get_image_size($src) { $CI =& get_instance(); $CI->load->helper('image'); $file = find_img($src); if ($file == null) { trigger_error('The responsive image can\'t be found'); return ''; } if (extension_loaded('imagick')) { $img = new Imagick($file); return $img->getImageGeometry(); } if (extension_loaded('gd')) { $path_parts = pathinfo($file); $ext = $path_parts['extension']; if ($ext == 'jpg' || $ext == 'jpeg') { $src_img = imagecreatefromjpeg($file); } else { $src_img = imagecreatefrompng($file); } return array('width' => imageSX($src_img), 'height' => imageSY($src_img)); } return array(0, 0); }
function inner_create_image_thumbnail($orig, $width, $height = 0) { $output_dir = 'application/cache/img/' . $width . 'x' . $height . '/'; // All the thumbnails is located in cache $out_file = find_file($orig, $output_dir); // Try to find the file from output dir if ($out_file != null) { // If the cached file is exists, return it return $out_file; } $file = find_img($orig); if ($file == null) { // We can't read the file { return null; } if ($width == 'normal') { return $file; } if (!file_exists($output_dir)) { // If the output dir is not exists, then create it mkdir($output_dir, 0777, true); } $path_parts = pathinfo($file); $name = $path_parts['filename']; $ext = $path_parts['extension']; $out_file = FCPATH . $output_dir . $orig; $dir = dirname($out_file); if (!file_exists($dir)) { mkdir($dir, 0755, true); } if (extension_loaded('imagick')) { Imagick::setResourceLimit(6, 1); // Set the thread limit to image magick $img = new Imagick($file); $img->thumbnailImage($width, $height); $img->writeImage($out_file); return $out_file; } if (extension_loaded('gd')) { if ($ext == 'jpg' || $ext == 'jpeg') { $src_img = imagecreatefromjpeg($file); } else { $src_img = imagecreatefrompng($file); } $old_x = imageSX($src_img); $old_y = imageSY($src_img); if ($height == 0) { $height = $old_y * $width / $old_x; } $dst_img = ImageCreateTrueColor($width, $height); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $old_x, $old_y); if ($ext == 'jpg' || $ext == 'jpeg') { imagejpeg($dst_img, $out_file); } else { imagepng($dst_img, $out_file); } imagedestroy($dst_img); imagedestroy($src_img); return $out_file; } return null; }