function createThumbnail($image, $size, $complete_tag = false, $alt = 'alt=""') { // Parse size parameter if (preg_match('@^(\\d+)x(\\d+)$@', $size, $matches)) { $width = $matches[1]; $height = $matches[2]; } else { if (preg_match('@^(\\d+)$@', $size)) { $width = $size; $height = $size; } else { MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Size "' . htmlspecialchars($size) . '" is not acceptable.', __FILE__, __LINE__); return MARKET_Filter::_defaultThumbnail(); } } $type = strtolower(substr(strrchr($image, '.'), 1)); $out = '/cache/' . dirname($image) . '/' . substr(basename($image), 0, strrpos(basename($image), '.')) . '.' . $width . 'x' . $height . '.' . $type; if (@is_file(MARKET_ROOT_DIR . '/' . $out) && @is_readable(MARKET_ROOT_DIR . '/' . $out)) { // Do nothing } else { // Create thumbnail // Requires GD if (extension_loaded('gd')) { $gd_info = gd_info(); $in = MARKET_ROOT_DIR . '/' . $image; if (@is_file($in) && @is_readable($in)) { if (!(list($w, $h) = getimagesize($in))) { MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" is not supported', __FILE__, __LINE__); return MARKET_Filter::_defaultThumbnail(); } if ($type == 'jpeg') { $type = 'jpg'; } switch ($type) { case 'bmp': $img = imagecreatefromwbmp($in); break; case 'gif': $img = imagecreatefromgif($in); break; case 'jpg': $img = imagecreatefromjpeg($in); break; case 'png': $img = imagecreatefrompng($in); break; default: MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" is not supported', __FILE__, __LINE__); return MARKET_Filter::_defaultThumbnail(); } // Resize and crop $sratio = $w / $h; $dratio = $width / $height; if ($sratio > $dratio) { $temp_width = (int) ($h * $dratio); $temp_height = $h; $x = (int) (($w - $temp_width) / 2); $y = 0; } else { $temp_width = $w; $temp_height = (int) ($w / $dratio); $x = 0; $y = (int) (($h - $temp_height) / 2); } $source_width = $temp_width; $source_height = $temp_height; $dst = imagecreatetruecolor($width, $height); // Preserve transparency if ($type == 'gif' || $type == 'png') { imagecolortransparent($dst, imagecolorallocatealpha($dst, 0, 0, 0, 127)); imagealphablending($dst, false); imagesavealpha($dst, true); } imagecopyresampled($dst, $img, 0, 0, $x, $y, $width, $height, $source_width, $source_height); // Create dir MARKET_Base::makeDir(dirname(MARKET_ROOT_DIR . $out)); switch ($type) { case 'bmp': imagewbmp($dst, MARKET_ROOT_DIR . $out); break; case 'gif': imagegif($dst, MARKET_ROOT_DIR . $out); break; case 'jpg': imagejpeg($dst, MARKET_ROOT_DIR . $out); break; case 'png': imagepng($dst, MARKET_ROOT_DIR . $out); break; } } else { MARKET_Base::raiseError(MARKET_ERROR_WARNING, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" not found or not readable', __FILE__, __LINE__); return MARKET_Filter::_defaultThumbnail('', $width, $height, $complete_tag, $alt); } } else { MARKET_Base::raiseError(MARKET_ERROR_WARNING, __FUNCTION__ . '(): The GD extension is not loaded', __FILE__, __LINE__); return MARKET_Filter::_defaultThumbnail($image, $width, $height, $complete_tag, $alt); } } if ($complete_tag) { return '<img src="' . MARKET_WEB_DIR . $out . '" width="' . $width . '" height="' . $height . '" ' . $alt . ' />'; } else { return MARKET_WEB_DIR . '/' . $out; } }