/** * Creates the cache folder version of the image, including watermarking * * @param string $newfilename the name of the file when it is in the cache * @param string $imgfile the image name * @param array $args the cropping arguments * @param bool $allow_watermark set to true if image may be watermarked * @param bool $force_cache set to true to force the image into the cache folders * @param string $theme the current theme */ function cacheImage($newfilename, $imgfile, $args, $allow_watermark = false, $force_cache = false, $theme) { @(list($size, $width, $height, $cw, $ch, $cx, $cy, $quality, $thumb, $crop, $thumbstandin, $thumbWM, $adminrequest) = $args); // Set the config variables for convenience. $image_use_side = getOption('image_use_side'); $upscale = getOption('image_allow_upscale'); $allowscale = true; $sharpenthumbs = getOption('thumb_sharpen'); $sharpenimages = getOption('image_sharpen'); $newfile = SERVERCACHE . $newfilename; if (DEBUG_IMAGE) { debugLog("cacheImage(\$imgfile=" . basename($imgfile) . ", \$newfilename={$newfilename}, \$allow_watermark={$allow_watermark}, \$force_cache={$force_cache}, \$theme={$theme}) \$size={$size}, \$width={$width}, \$height={$height}, \$cw={$cw}, \$ch={$ch}, \$cx={$cx}, \$cy={$cy}, \$quality={$quality}, \$thumb={$thumb}, \$crop={$crop} \$image_use_side={$image_use_side}; \$upscale={$upscale};"); } // Check for GD if (!function_exists('imagecreatetruecolor')) { imageError(gettext('The GD Library is not installed or not available.'), 'err-nogd.gif'); } // Check for the source image. if (!file_exists($imgfile) || !is_readable($imgfile)) { imageError(gettext('Image not found or is unreadable.'), 'err-imagenotfound.gif'); } $rotate = false; if (function_exists('imagerotate') && getOption('auto_rotate')) { $rotate = getImageRotation($imgfile); } if ($im = get_image($imgfile)) { if ($rotate) { $newim_rot = imagerotate($im, $rotate, 0); imagedestroy($im); $im = $newim_rot; } $w = imagesx($im); $h = imagesy($im); // Give the sizing dimension to $dim $ratio_in = ''; $ratio_out = ''; $crop = $crop || $cw != 0 || $ch != 0; if (!empty($size)) { $dim = $size; $width = $height = false; if ($crop) { $dim = $size; if (!$ch) { $ch = $size; } if (!$cw) { $cw = $size; } } } else { if (!empty($width) && !empty($height)) { $ratio_in = $h / $w; $ratio_out = $height / $width; if ($ratio_in > $ratio_out) { // image is taller than desired, $height is the determining factor $thumb = true; $dim = $width; if (!$ch) { $ch = $height; } } else { // image is wider than desired, $width is the determining factor $dim = $height; if (!$cw) { $cw = $width; } } } else { if (!empty($width)) { $dim = $width; $size = $height = false; } else { if (!empty($height)) { $dim = $height; $size = $width = false; } else { // There's a problem up there somewhere... imageError(gettext("Unknown error! Please report to the developers at <a href=\"http://www.zenphoto.org/\">www.zenphoto.org</a>"), 'err-imagegeneral.gif'); } } } } $sizes = propSizes($size, $width, $height, $w, $h, $thumb, $image_use_side, $dim); list($neww, $newh) = $sizes; if (DEBUG_IMAGE) { debugLog("cacheImage:" . basename($imgfile) . ": \$size={$size}, \$width={$width}, \$height={$height}, \$w={$w}; \$h={$h}; \$cw={$cw}, \$ch={$ch}, \$cx={$cx}, \$cy={$cy}, \$quality={$quality}, \$thumb={$thumb}, \$crop={$crop}, \$newh={$newh}, \$neww={$neww}, \$dim={$dim}, \$ratio_in={$ratio_in}, \$ratio_out={$ratio_out} \$upscale={$upscale} \$rotate={$rotate} \$force_cache={$force_cache}"); } if (!$upscale && $newh >= $h && $neww >= $w) { // image is the same size or smaller than the request if (!getOption('watermark_image') && !($crop || $thumb || $rotate || $force_cache)) { // no processing needed if (DEBUG_IMAGE) { debugLog("Serve " . basename($imgfile) . " from original image."); } if (getOption('album_folder_class') != 'external') { // local album system, return the image directly $image = substr(strrchr($imgfile, '/'), 1); $album = substr($imgfile, strlen(getAlbumFolder())); $album = substr($album, 0, strlen($album) - strlen($image) - 1); header("Location: " . getAlbumFolder(FULLWEBPATH) . pathurlencode($album) . "/" . rawurlencode($image)); exit; } else { // the web server does not have access to the image, have to supply it $suffix = strtolower(substr(strrchr($filename, "."), 1)); $fp = fopen($imgfile, 'rb'); // send the right headers header("Content-Type: image/{$suffix}"); header("Content-Length: " . filesize($imgfile)); // dump the picture and stop the script fpassthru($fp); fclose($fp); exit; } } $neww = $w; $newh = $h; $allowscale = false; if ($crop) { if ($width > $neww) { $width = $neww; } if ($height > $newh) { $height = $newh; } } if (DEBUG_IMAGE) { debugLog("cacheImage:no upscale " . basename($imgfile) . ": \$newh={$newh}, \$neww={$neww}"); } } // Crop the image if requested. if ($crop) { if ($cw > $ch) { $ir = $ch / $cw; } else { $ir = $cw / $ch; } if ($size) { $ts = $size; $neww = $size; $newh = $ir * $size; } else { $neww = $width; $newh = $height; if ($neww > $newh) { $ts = $neww; if ($newh === false) { $newh = $ir * $neww; } } else { $ts = $newh; if ($neww === false) { $neww = $ir * $newh; } } } $cr = min($w, $h) / $ts; if (!$cx) { if (!$cw) { $cw = $w; } else { $cw = round($cw * $cr); } $cx = round(($w - $cw) / 2); } else { // custom crop if (!$cw || $cw > $w) { $cw = $w; } } if (!$cy) { if (!$ch) { $ch = $h; } else { $ch = round($ch * $cr); } $cy = round(($h - $ch) / 2); } else { // custom crop if (!$ch || $ch > $h) { $ch = $h; } } if ($cw + $cx > $w) { $cx = $w - $cw; } if ($cx < 0) { $cw = $cw + $cx; $cx = 0; } if ($ch + $cy > $h) { $cy = $h - $ch; } if ($cy < 0) { $ch = $ch + $cy; $cy = 0; } if (DEBUG_IMAGE) { debugLog("cacheImage:crop " . basename($imgfile) . ":\$size={$size}, \$width={$width}, \$height={$height}, \$cw={$cw}, \$ch={$ch}, \$cx={$cx}, \$cy={$cy}, \$quality={$quality}, \$thumb={$thumb}, \$crop={$crop}, \$rotate={$rotate}"); } $newim = imagecreatetruecolor($neww, $newh); imagecopyresampled($newim, $im, 0, 0, $cx, $cy, $neww, $newh, $cw, $ch); } else { if ($allowscale) { $sizes = propSizes($size, $width, $height, $w, $h, $thumb, $image_use_side, $dim); list($neww, $newh) = $sizes; } if (DEBUG_IMAGE) { debugLog("cacheImage:no crop " . basename($imgfile) . ":\$size={$size}, \$width={$width}, \$height={$height}, \$dim={$dim}, \$neww={$neww}; \$newh={$newh}; \$quality={$quality}, \$thumb={$thumb}, \$crop={$crop}, \$rotate={$rotate}; \$allowscale={$allowscale};"); } $newim = imagecreatetruecolor($neww, $newh); imagecopyresampled($newim, $im, 0, 0, 0, 0, $neww, $newh, $w, $h); } if ($thumb && $sharpenthumbs || !$thumb && $sharpenimages) { unsharp_mask($newim, getOption('sharpen_amount'), getOption('sharpen_radius'), getOption('sharpen_threshold')); } $watermark_image = false; if ($thumbWM) { if ($thumb || !$allow_watermark) { $watermark_image = SERVERPATH . '/' . ZENFOLDER . '/watermarks/' . UTF8ToFileSystem($thumbWM) . '.png'; if (!file_exists($watermark_image)) { $watermark_image = SERVERPATH . '/' . ZENFOLDER . '/images/imageDefault.png'; } } } else { if ($allow_watermark) { $watermark_image = getOption('fullimage_watermark'); if ($watermark_image) { $watermark_image = SERVERPATH . '/' . ZENFOLDER . '/watermarks/' . UTF8ToFileSystem($watermark_image) . '.png'; if (!file_exists($watermark_image)) { $watermark_image = SERVERPATH . '/' . ZENFOLDER . '/images/imageDefault.png'; } } } } if ($watermark_image) { $offset_h = getOption('watermark_h_offset') / 100; $offset_w = getOption('watermark_w_offset') / 100; $watermark = imagecreatefrompng($watermark_image); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $imw = imagesx($newim); $imh = imagesy($newim); $percent = getOption('watermark_scale') / 100; $r = sqrt($imw * $imh * $percent / ($watermark_width * $watermark_height)); if (!getOption('watermark_allow_upscale')) { $r = min(1, $r); } $nw = round($watermark_width * $r); $nh = round($watermark_height * $r); if ($nw != $watermark_width || $nh != $watermark_height) { $watermark = imageResizeAlpha($watermark, $nw, $nh); } // Position Overlay in Bottom Right $dest_x = max(0, floor(($imw - $nw) * $offset_w)); $dest_y = max(0, floor(($imh - $nh) * $offset_h)); if (DEBUG_IMAGE) { debugLog("Watermark:" . basename($imgfile) . ": \$offset_h={$offset_h}, \$offset_w={$offset_w}, \$watermark_height={$watermark_height}, \$watermark_width={$watermark_width}, \$imw={$imw}, \$imh={$imh}, \$percent={$percent}, \$r={$r}, \$nw={$nw}, \$nh={$nh}, \$dest_x={$dest_x}, \$dest_y={$dest_y}"); } imagecopy($newim, $watermark, $dest_x, $dest_y, 0, 0, $nw, $nh); imagedestroy($watermark); } // Create the cached file (with lots of compatibility)... mkdir_recursive(dirname($newfile)); if (imagejpeg($newim, $newfile, $quality)) { if (DEBUG_IMAGE) { debugLog('Finished:' . basename($imgfile)); } } else { if (DEBUG_IMAGE) { debugLog('cacheImage: failed to create ' . $newfile); } } @chmod($newfile, 0666 & CHMOD_VALUE); imagedestroy($newim); imagedestroy($im); } }
/** * Функция для работы с изображениями PNG, для сохранения Alpha - канала при уменьшении картинки * * @param resource $src Ресурс картинки * @param resource $ovr Возвращает работу функции imageResizeAlpha() * @param integer $ovr_x Ширина начального файла * @param integer $ovr_y Высота начального файла * @param integer $ovr_w Ширина конечного файла (если false не учитывается) * @param integer $ovr_h Высота конечного файла (если fakse не учитывается) */ function imageComposeAlpha(&$src, &$ovr, $ovr_x, $ovr_y, $ovr_w = false, $ovr_h = false) { if ($ovr_w && $ovr_h) { $ovr = imageResizeAlpha($ovr, $ovr_w, $ovr_h); } /* Noew compose the 2 images */ imagecopy($src, $ovr, $ovr_x, $ovr_y, 0, 0, imagesx($ovr), imagesy($ovr)); }
$offset_h = getOption('watermark_h_offset') / 100; $offset_w = getOption('watermark_w_offset') / 100; $watermark = imagecreatefrompng($watermark_path); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $imw = imagesx($newim); $imh = imagesy($newim); $percent = getOption('watermark_scale') / 100; $r = sqrt($imw * $imh * $percent / ($watermark_width * $watermark_height)); if (!getOption('watermark_allow_upscale')) { $r = min(1, $r); } $nw = round($watermark_width * $r); $nh = round($watermark_height * $r); if ($nw != $watermark_width || $nh != $watermark_height) { $watermark = imageResizeAlpha($watermark, $nw, $nh); } // Position Overlay in Bottom Right $dest_x = max(0, floor(($imw - $nw) * $offset_w)); $dest_y = max(0, floor(($imh - $nh) * $offset_h)); imagecopy($newim, $watermark, $dest_x, $dest_y, 0, 0, $nw, $nh); imagedestroy($watermark); } $quality = getOption('full_image_quality'); switch ($suffix) { case 'jpeg': imagejpeg($newim, $cache_path, $quality); break; case 'png': if ($quality = 100) { $quality = 0;