function createThumbnail($path, $config, $k2_mode = false, $vm_mode = false, $img_rel = '') { // importing classes jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.path'); //script configuration - increase memory limit to selected value ini_set('memory_limit', $config['memory_limit']); // cache dir $cache_dir = JPATH_ROOT . DS . 'modules' . DS . 'mod_news_pro_gk4' . DS . 'cache' . DS; // checking the special images $check_result = NSP_GK4_Thumbs::checkSpecialImages(NSP_GK4_Thumbs::translateName($path, $config['module_id'], $k2_mode, $vm_mode)); // preparing an array with the image class values $img_rels = array(); // check if any classes exists in the image if ($img_rel !== '') { $img_rels = explode(' ', $img_rel); } // no scale images if ($check_result == 2) { // NOSCALE if (NSP_GK4_Thumbs::checkCache(NSP_GK4_Thumbs::translateName($path, $config['module_id'], $k2_mode, $vm_mode, '_noscale'), $config['cache_time'], $config['module_id'])) { return TRUE; } else { // file path $file = NSP_GK4_Thumbs::getRealPath($path, $k2_mode, $vm_mode, '_noscale'); // filename $filename = NSP_GK4_Thumbs::translateName($path, $config['module_id'], $k2_mode, $vm_mode, '_noscale'); // Getting informations about image if (is_file($file)) { $imageData = getimagesize($file); $img_w = str_replace('px', '', str_replace('%', '', $config['img_width'])); $img_h = str_replace('px', '', str_replace('%', '', $config['img_height'])); // loading image depends from type of image if ($imageData['mime'] == 'image/jpeg' || $imageData['mime'] == 'image/pjpeg' || $imageData['mime'] == 'image/jpg') { $imageSource = @imagecreatefromjpeg($file); } elseif ($imageData['mime'] == 'image/gif') { $imageSource = @imagecreatefromgif($file); } else { $imageSource = @imagecreatefrompng($file); } // here can be exist an error when image is to big - then class return blank page // setting image size in variables $imageSourceWidth = imagesx($imageSource); $imageSourceHeight = imagesy($imageSource); $imageBG = imagecreatetruecolor($img_w, $img_h); // If image is JPG or GIF if ($imageData['mime'] == 'image/png') { $imageBG = imagecreatetruecolor($img_w, $img_h); // enable transparent background if ($config['img_bg'] == 'transparent') { // create transparent color $rgb = imagecolorallocatealpha($imageBG, 0, 0, 0, 127); } else { // create normal color $bg = $config['img_bg']; // translate hex to RGB $hex_color = strtolower(trim($bg, '#;&Hh')); $bg = array_map('hexdec', explode('.', wordwrap($hex_color, ceil(strlen($hex_color) / 3), '.', 1))); // creating color $rgb = imagecolorallocate($imageBG, $bg[0], $bg[1], $bg[2]); } // filling the canvas imagefill($imageBG, 0, 0, $rgb); // enabling transparent settings for better quality imagealphablending($imageBG, false); imagesavealpha($imageBG, true); } // $imageSourceNWidth = $img_w; $imageSourceNHeight = $img_h; $base_x = 0; $base_y = 0; // copy image imagecopyresampled($imageBG, $imageSource, $base_x, $base_y, 0, 0, $imageSourceNWidth, $imageSourceNHeight, $imageSourceWidth, $imageSourceHeight); // save image depends from MIME type if ($imageData['mime'] == 'image/jpeg' || $imageData['mime'] == 'image/pjpeg' || $imageData['mime'] == 'image/jpg') { imagejpeg($imageBG, $cache_dir . $filename, $config['img_quality']); } elseif ($imageData['mime'] == 'image/gif') { imagegif($imageBG, $cache_dir . $filename); } else { imagepng($imageBG, $cache_dir . $filename); } // result return TRUE; } else { return FALSE; } } } else { // cropped or normal image if (NSP_GK4_Thumbs::checkCache(NSP_GK4_Thumbs::translateName($path, $config['module_id'], $k2_mode, $vm_mode, $check_result == 1 ? '_cropped' : ''), $config['cache_time'], $config['module_id'])) { return TRUE; } else { // file path $file = NSP_GK4_Thumbs::getRealPath($path, $k2_mode, $vm_mode, $check_result == 1 ? '_cropped' : ''); // filename $filename = NSP_GK4_Thumbs::translateName($path, $config['module_id'], $k2_mode, $vm_mode, $check_result == 1 ? '_cropped' : ''); // Getting informations about image if (is_file($file)) { $imageData = getimagesize($file); $img_w = str_replace('px', '', str_replace('%', '', $config['img_width'])); $img_h = str_replace('px', '', str_replace('%', '', $config['img_height'])); // loading image depends from type of image if ($imageData['mime'] == 'image/jpeg' || $imageData['mime'] == 'image/pjpeg' || $imageData['mime'] == 'image/jpg') { $imageSource = @imagecreatefromjpeg($file); } elseif ($imageData['mime'] == 'image/gif') { $imageSource = @imagecreatefromgif($file); } else { $imageSource = @imagecreatefrompng($file); } // here can be exist an error when image is to big - then class return blank page // setting image size in variables $imageSourceWidth = imagesx($imageSource); $imageSourceHeight = imagesy($imageSource); // variables for cropping $top_crop = floor($imageSourceHeight * $config['simple_crop_top'] / 100); $bottom_crop = floor($imageSourceHeight * $config['simple_crop_bottom'] / 100); $left_crop = floor($imageSourceWidth * $config['simple_crop_left'] / 100); $right_crop = floor($imageSourceWidth * $config['simple_crop_right'] / 100); // get the cropping rules foreach ($config['crop_rules'] as $rule) { if (is_string($rule['type']) && count($img_rels)) { foreach ($img_rels as $rel) { if ($rule['type'] == $rel) { $top_crop = $rule['top']; $bottom_crop = $rule['bottom']; $left_crop = $rule['left']; $right_crop = $rule['right']; break; } } } else { if ($rule['type']['width'] == $imageSourceWidth && $rule['type']['height'] == $imageSourceHeight) { $top_crop = $rule['top']; $bottom_crop = $rule['bottom']; $left_crop = $rule['left']; $right_crop = $rule['right']; break; } } } // Creating blank canvas if ($config['img_keep_aspect_ratio']) { // calculate ratio for first scaling $ratio = $imageSourceWidth > $imageSourceHeight ? $img_w / $imageSourceWidth : $img_h / $imageSourceHeight; // calculate new image size $imageSourceNWidth = ($imageSourceWidth - ($left_crop + $right_crop)) * $ratio; $imageSourceNHeight = ($imageSourceHeight - ($top_crop + $bottom_crop)) * $ratio; // calculate ratio for second scaling if ($img_w > $img_h) { if ($imageSourceNHeight > $img_h) { $ratio2 = $img_h / $imageSourceNHeight; $imageSourceNHeight *= $ratio2; $imageSourceNWidth *= $ratio2; } } else { if ($imageSourceNWidth > $img_w) { $ratio2 = $img_w / $imageSourceNWidth; $imageSourceNHeight *= $ratio2; $imageSourceNWidth *= $ratio2; } } $img_w = $imageSourceNWidth; $img_h = $imageSourceNHeight; } $imageBG = imagecreatetruecolor($img_w, $img_h); // If image is JPG or GIF if ($imageData['mime'] == 'image/jpeg' || $imageData['mime'] == 'image/pjpeg' || $imageData['mime'] == 'image/jpg' || $imageData['mime'] == 'image/gif') { // when bg is set to transparent - use black background if ($config['img_bg'] == 'transparent') { $bgColorR = 0; $bgColorG = 0; $bgColorB = 0; } else { // in other situation - translate hex to RGB $bg = $config['img_bg']; if (strlen($bg) == 4) { $bg = $bg[0] . $bg[1] . $bg[1] . $bg[2] . $bg[2] . $bg[3] . $bg[3]; } $hex_color = strtolower(trim($bg, '#;&Hh')); $bg = array_map('hexdec', explode('.', wordwrap($hex_color, ceil(strlen($hex_color) / 3), '.', 1))); $bgColorR = $bg[0]; $bgColorG = $bg[1]; $bgColorB = $bg[2]; } // Creating color $rgb = imagecolorallocate($imageBG, $bgColorR, $bgColorG, $bgColorB); // filling canvas with new color imagefill($imageBG, 0, 0, $rgb); } else { // for PNG images if ($config['img_keep_aspect_ratio']) { // calculate ratio for first scaling $ratio = $imageSourceWidth > $imageSourceHeight ? $img_w / $imageSourceWidth : $img_h / $imageSourceHeight; // calculate new image size $imageSourceNWidth = ($imageSourceWidth - ($left_crop + $right_crop)) * $ratio; $imageSourceNHeight = ($imageSourceHeight - ($top_crop + $bottom_crop)) * $ratio; // calculate ratio for second scaling if ($img_w > $img_h) { if ($imageSourceNHeight > $img_h) { $ratio2 = $img_h / $imageSourceNHeight; $imageSourceNHeight *= $ratio2; $imageSourceNWidth *= $ratio2; } } else { if ($imageSourceNWidth > $img_w) { $ratio2 = $img_w / $imageSourceNWidth; $imageSourceNHeight *= $ratio2; $imageSourceNWidth *= $ratio2; } } $img_w = $imageSourceNWidth; $img_h = $imageSourceNHeight; } $imageBG = imagecreatetruecolor($img_w, $img_h); // enable transparent background if ($config['img_bg'] == 'transparent') { // create transparent color $rgb = imagecolorallocatealpha($imageBG, 0, 0, 0, 127); } else { // create normal color $bg = $config['img_bg']; // translate hex to RGB $hex_color = strtolower(trim($bg, '#;&Hh')); $bg = array_map('hexdec', explode('.', wordwrap($hex_color, ceil(strlen($hex_color) / 3), '.', 1))); // creating color $rgb = imagecolorallocate($imageBG, $bg[0], $bg[1], $bg[2]); } // filling the canvas imagefill($imageBG, 0, 0, $rgb); // enabling transparent settings for better quality imagealphablending($imageBG, false); imagesavealpha($imageBG, true); } // when stretching is disabled if (!$config['img_stretch'] || $config['img_keep_aspect_ratio']) { if ($config['img_keep_aspect_ratio']) { $base_x = 0; $base_y = 0; $imageSourceNWidth = $img_w; $imageSourceNHeight = $img_h; } else { // if image auto-scale is enabled if ($config['img_auto_scale']) { $img_ratio = $imageSourceWidth / $imageSourceHeight; $thumb_ratio = $img_w / $img_h; // base variables $imageSourceNWidth = $img_w; $imageSourceNHeight = $img_h; $base_x = 0; $base_y = 0; // if img_ratio == thumb_ratio - no scale and cuts $left_crop = 0; $top_crop = 0; // two other cases if ($img_ratio > $thumb_ratio) { // when source is wider than destination $ratio_h = $imageSourceHeight / $img_h; $new_w = $img_w * $ratio_h; $left_crop = floor(($imageSourceWidth - $new_w) / 2); // top_crop still 0 } else { if ($img_ratio < $thumb_ratio) { // when source is higher than destination $ratio_w = $imageSourceWidth / $img_w; $new_h = $img_h * $ratio_w; $top_crop = floor(($imageSourceHeight - $new_h) / 2); // left_crop still } } // equal two other crop params $right_crop = $left_crop; $bottom_crop = $top_crop; } else { // if not - left unused areas of the image // calculate ratio for first scaling $ratio = $imageSourceWidth - ($left_crop + $right_crop) > $imageSourceHeight - ($top_crop + $bottom_crop) ? $img_w / ($imageSourceWidth - ($left_crop + $right_crop)) : $img_h / ($imageSourceHeight - ($top_crop + $bottom_crop)); // calculate new image size $imageSourceNWidth = ($imageSourceWidth - ($left_crop + $right_crop)) * $ratio; $imageSourceNHeight = ($imageSourceHeight - ($top_crop + $bottom_crop)) * $ratio; // calculate ratio for second scaling if ($img_w > $img_h) { if ($imageSourceNHeight > $img_h) { $ratio2 = $img_h / $imageSourceNHeight; $imageSourceNHeight *= $ratio2; $imageSourceNWidth *= $ratio2; } } else { if ($imageSourceNWidth > $img_w) { $ratio2 = $img_w / $imageSourceNWidth; $imageSourceNHeight *= $ratio2; $imageSourceNWidth *= $ratio2; } } // setting position of putting thumbnail on canvas $base_x = floor(($img_w - $imageSourceNWidth) / 2); $base_y = floor(($img_h - $imageSourceNHeight) / 2); } } } else { // when stretching is enable $imageSourceNWidth = $img_w; $imageSourceNHeight = $img_h; $base_x = 0; $base_y = 0; } // copy image imagecopyresampled($imageBG, $imageSource, $base_x, $base_y, $left_crop, $top_crop, $imageSourceNWidth, $imageSourceNHeight, $imageSourceWidth - ($left_crop + $right_crop), $imageSourceHeight - ($top_crop + $bottom_crop)); // save image depends from MIME type if ($imageData['mime'] == 'image/jpeg' || $imageData['mime'] == 'image/pjpeg' || $imageData['mime'] == 'image/jpg') { imagejpeg($imageBG, $cache_dir . $filename, $config['img_quality']); } elseif ($imageData['mime'] == 'image/gif') { imagegif($imageBG, $cache_dir . $filename); } else { imagepng($imageBG, $cache_dir . $filename); } // result return TRUE; } else { return FALSE; } } } }
function image_vm($config, $news_id, $news_cid, $news_image, $news_title) { $news_title = str_replace('"', """, $news_title); $IMG_SOURCE = JURI::root() . $news_image; $IMG_LINK = 'index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=' . $news_id . '&virtuemart_category_id=' . $news_cid . '&Itemid=' . $itemid; if (preg_match('/\\<img.*src=.*?\\>/', $news_text)) { $imgStartPos = JString::strpos($news_text, 'src="'); if ($imgStartPos) { $imgEndPos = JString::strpos($news_text, '"', $imgStartPos + 5); } if ($imgStartPos > 0) { $IMG_SOURCE = JString::substr($news_text, $imgStartPos + 5, $imgEndPos - ($imgStartPos + 5)); } } // if ($config['create_thumbs'] == 1 && $IMG_SOURCE != '') { // try to override standard image if (strpos($IMG_SOURCE, 'http://') == FALSE) { if (NSP_GK4_Thumbs::createThumbnail($IMG_SOURCE, $config) !== FALSE) { $uri = JURI::getInstance(); $IMG_SOURCE = $uri->root() . 'modules/mod_news_pro_gk4/cache/' . NSP_GK4_Thumbs::translateName($IMG_SOURCE, $config['module_id']); } elseif ($config['create_thumbs'] == 1) { jimport('joomla.filesystem.file'); if (is_file(JPATH_ROOT . DS . 'modules' . DS . 'mod_news_pro_gk4' . DS . 'cache' . DS . 'default' . DS . 'default' . $config['module_id'] . '.png')) { $IMG_SOURCE = $uri->root() . 'modules/mod_news_pro_gk4/cache/default/default' . $config['module_id'] . '.png'; } } else { $IMG_SOURCE = ''; } } } elseif ($config['create_thumbs'] == 1) { jimport('joomla.filesystem.file'); if (is_file(JPATH_ROOT . DS . 'modules' . DS . 'mod_news_pro_gk4' . DS . 'cache' . DS . 'default' . DS . 'default' . $config['module_id'] . '.png')) { $IMG_SOURCE = $uri->root() . 'modules/mod_news_pro_gk4/cache/default/default' . $config['module_id'] . '.png'; } } // if ($IMG_SOURCE != '' && $config['news_content_image_pos'] != 'disabled') { $class = ' t' . $config['news_content_image_pos'] . ' f' . $config['news_content_image_float']; $size = ''; // if ($config['img_width'] != 0 && !$config['img_keep_aspect_ratio']) { $size .= 'width:' . $config['img_width'] . 'px;'; } if ($config['img_height'] != 0 && !$config['img_keep_aspect_ratio']) { $size .= 'height:' . $config['img_height'] . 'px;'; } if ($config['img_margin'] != '') { $size .= 'margin:' . $config['img_margin'] . ';'; } // if ($config['news_image_link'] == 1) { return $config['news_content_image_pos'] == 'center' ? '<div class="center' . $class . '"><a href="' . $IMG_LINK . '"><img class="nspImage" src="' . $IMG_SOURCE . '" alt="' . htmlspecialchars($news_title) . '" style="' . $size . '" /></a></div>' : '<a href="' . $IMG_LINK . '"><img class="nspImage' . $class . '" src="' . $IMG_SOURCE . '" alt="' . htmlspecialchars($news_title) . '" style="' . $size . '" /></a>'; } else { return $config['news_content_image_pos'] == 'center' ? '<div class="center' . $class . '"><img class="nspImage" src="' . $IMG_SOURCE . '" alt="' . htmlspecialchars($news_title) . '" ' . $size . ' /></div>' : '<img class="nspImage' . $class . '" src="' . $IMG_SOURCE . '" alt="' . htmlspecialchars($news_title) . '" style="' . $size . '" />'; } } else { return ''; } }