Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Cache i obrazki
Forum PHP.pl > Forum > PHP
Largo
Witam,

  1. <?php
  2. /**
  3.  * i.php: Zenphoto image processor
  4.  * All *uncached* image requests go through this file
  5.  * (As of 1.0.8 images are requested directly from the cache if they exist)
  6.  ***************************************************************************
    ****
  7.  * URI Parameters:
  8.  * s - size (logical): Based on config, makes an image of "size s."
  9.  * h - height (explicit): Image will be resized to h pixels high, w is calculated.
  10.  * w - width (explicit): Image will resized to w pixels wide, h is calculated.
  11.  * cw - crop width: crops the image to cw pixels wide.
  12.  * ch - crop height: crops the image to ch pixels high.
  13.  * cx - crop x position: the x (horizontal) position of the crop area.
  14.  * cy - crop y position: the y (vertical) position of the crop area.
  15.  * q - JPEG quality (1-100): sets the quality of the resulting image.
  16.  * t - Set for custom images if used as thumbs.
  17.  *
  18.  * Cropping is performed on the original image before resizing is done.
  19.  * - cx and cy are measured from the top-left corner of the image.
  20.  * - One of s, h, or w _must_ be specified; the others are optional.
  21.  * - If more than one of s, h, or w are specified, s takes priority, then w+h:
  22.  * - If none of s, h, or w are specified, the original image is returned.
  23.  ***************************************************************************
    ****
  24.  * @package core
  25.  */
  26.  
  27. // force UTF-8 O
  28.  
  29.  
  30. define('OFFSET_PATH', 2);
  31. require_once(dirname(__FILE__).'/functions-basic.php');
  32. require_once(dirname(__FILE__).'/functions-image.php');
  33.  
  34. $debug = isset($_GET['debug']);
  35.  
  36. // Check for minimum parameters.
  37. if (empty($_GET['a']) && empty($_GET['i'])) {
  38. header("HTTP/1.0 404 Not Found");
  39. header("Status: 404 Not Found");
  40. imageError(gettext("Too few arguments! Image not found."), 'err-imagenotfound.gif');
  41. }
  42. // Fix special characters in the album and image names if mod_rewrite is on:
  43. // URL looks like: "/album1/subalbum/image/picture.jpg"
  44.  
  45. list($ralbum, $rimage) = rewrite_get_album_image('a', 'i');
  46. $ralbum = internalToFilesystem($ralbum);
  47. $rimage = internalToFilesystem($rimage);
  48. $album = str_replace('..','', sanitize_path($ralbum));
  49. $image = str_replace(array('/',"\\"),'', sanitize_path($rimage));
  50. $theme = themeSetup($album); // loads the theme based image options.
  51. $adminrequest = isset($_GET['admin']);
  52.  
  53. // Extract the image parameters from the input variables
  54. // This validates the input as well.
  55. $args = array(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  56. if (isset($_GET['s'])) { //0
  57. $args[0] = min(abs($_GET['s']), MAX_SIZE);
  58. }
  59. if (isset($_GET['w'])) { //1
  60. $args[1] = min(abs($_GET['w']), MAX_SIZE);
  61. }
  62. if (isset($_GET['h'])) { //2
  63. $args[2] = min(abs($_GET['h']), MAX_SIZE);
  64. }
  65. if (isset($_GET['cw'])) { //3
  66. $args[3] = $_GET['cw'];
  67. }
  68. if (isset($_GET['ch'])) { //4
  69. $args[4] = $_GET['ch'];
  70. }
  71. if (isset($_GET['cx'])) { //5
  72. $args[5] = $_GET['cx'];
  73. }
  74. if (isset($_GET['cy'])) { //6
  75. $args[6] = $_GET['cy'];
  76. }
  77. if (isset($_GET['q'])) { //7
  78. $args[7] = $_GET['q'];
  79. }
  80. //8 thumb
  81. //9 crop
  82. if (isset($_GET['t'])) { //10
  83. $args[10] = $_GET['t'];
  84. }
  85. if (isset($_GET['wmk']) && !$adminrequest) { //11
  86. $args[11] = $_GET['wmk'];
  87. }
  88. $args [12] = $adminrequest; //12
  89.  
  90. if (isset($_GET['gray'])) { //13
  91. $args[13] = $_GET['gray'];
  92. }
  93.  
  94. if ( !isset($_GET['s']) && !isset($_GET['w']) && !isset($_GET['h'])) {
  95. // No image parameters specified
  96. if (getOption('album_folder_class') !== 'external') {
  97. header("Location: " . getAlbumFolder(FULLWEBPATH) . pathurlencode(filesystemToInternal($album)) . "/" . rawurlencode(filesystemToInternal($image)));
  98. return;
  99. }
  100. // external album, Web server cannot serve original image. Force resize to as big as we can do
  101. $args[0] = MAX_SIZE;
  102. }
  103. $args = getImageParameters($args,filesystemToInternal($album));
  104. list($size, $width, $height, $cw, $ch, $cx, $cy, $quality, $thumb, $crop, $thumbstandin, $passedWM, $adminrequest, $gray) = $args;
  105. if (DEBUG_IMAGE) debugLog("i.php($ralbum, $rimage): \$size=$size, \$width=$width, \$height=$height, \$cw=$cw, \$ch=$ch, \$cx=$cx, \$cy=$cy, \$quality=$quality, \$thumb=$thumb, \$crop=$crop, \$thumbstandin=$thumbstandin, \$passedWM=$passedWM, \$adminrequest=$adminrequest, \$gray=$gray");
  106. $allowWatermark = !$thumb && !$adminrequest;
  107.  
  108. // Construct the filename to save the cached image.
  109. $newfilename = getImageCacheFilename(filesystemToInternal($album), filesystemToInternal($image), $args);
  110. $newfile = SERVERCACHE . $newfilename;
  111.  
  112. if (trim($album)=='') {
  113. $imgfile = getAlbumFolder() . $image;
  114. } else {
  115. $imgfile = getAlbumFolder() . $album.'/'.$image;
  116. }
  117. if ($debug) imageDebug($album, $image, $args, $imgfile);
  118.  
  119. /** Check for possible problems ***********
  120.  ******************************************/
  121. // Make sure the cache directory is writable, attempt to fix. Issue a warning if not fixable.
  122. if (!is_dir(SERVERCACHE)) {
  123. @mkdir(SERVERCACHE, CHMOD_VALUE);
  124. @chmod(SERVERCACHE, CHMOD_VALUE);
  125. if (!is_dir(SERVERCACHE))
  126. imageError(gettext("The cache directory does not exist. Please create it and set the permissions to 0777."), 'err-cachewrite.gif');
  127. }
  128. if (!is_writable(SERVERCACHE)) {
  129. @chmod(SERVERCACHE, CHMOD_VALUE);
  130. if (!is_writable(SERVERCACHE))
  131. imageError(gettext("The cache directory is not writable! Attempts to chmod didn't work."), 'err-cachewrite.gif');
  132. }
  133. if (!file_exists($imgfile)) {
  134. $imgfile = $rimage; // undo the sanitize
  135. // then check to see if it is a transient image
  136. $i = strpos($imgfile, '_{');
  137. if ($i !== false) {
  138. $j = strpos($imgfile, '}_');
  139. $source = substr($imgfile, $i+2, $j-$i-2);
  140. $imgfile = substr($imgfile, $j+1);
  141. $i = strpos($imgfile, '_{');
  142. if ($i !== false) {
  143. $j = strpos($imgfile, '}_');
  144. $source2 = '/'.substr($imgfile, $i+2, $j-$i-2);
  145. $imgfile = substr($imgfile, $j+2);
  146. } else {
  147. $source2 = '';
  148. }
  149.  
  150. if ($source != ZENFOLDER) {
  151. $source = THEMEFOLDER.'/'.$source;
  152. }
  153. $args[3] = $args[4] = 0;
  154. $args[5] = 1; // full crops for these default images
  155. $args[9] = NULL;
  156. $imgfile = SERVERPATH .'/'. $source.$source2 . "/" . $imgfile;
  157.  
  158. }
  159. if (!file_exists($imgfile)) {
  160. header("HTTP/1.0 404 Not Found");
  161. header("Status: 404 Not Found");
  162. imageError(gettext("Image not found; file does not exist."), 'err-imagenotfound.gif');
  163. }
  164. }
  165. // Make the directories for the albums in the cache, recursively.
  166. // Skip this for safe_mode, where we can't write to directories we create!
  167. if (!ini_get("safe_mode")) {
  168. $albumdirs = getAlbumArray($album, true);
  169. foreach($albumdirs as $dir) {
  170. $dir = internalToFilesystem($dir);
  171. $dir = SERVERCACHE . '/' . $dir;
  172. if (!is_dir($dir)) {
  173. @mkdir($dir, CHMOD_VALUE);
  174. @chmod($dir, CHMOD_VALUE);
  175. } else if (!is_writable($dir)) {
  176. @chmod($dir, CHMOD_VALUE);
  177. }
  178. }
  179. }
  180.  
  181. $process = true;
  182. // If the file exists, check its modification time and update as needed.
  183. $fmt = filemtime($imgfile);
  184. if (file_exists($newfile) & !$adminrequest) {
  185. if (filemtime($newfile) >= filemtime($imgfile)) {
  186. $process = false;
  187. }
  188. }
  189. if ($process) { // If the file hasn't been cached yet, create it.
  190. // setup standard image options from the album theme if it exists
  191. if (!cacheImage_protected($newfilename, $imgfile, $args, $allowWatermark, false, $theme, $album)) {
  192. imageError(gettext('Image processing resulted in a fatal error.'));
  193. }
  194. $fmt = filemtime($newfile);
  195. }
  196. $path = $_zp_conf_vars['cache_folder'] . internalToFilesystem(imgSrcURI($newfilename));
  197. if (!$debug) {
  198. if(file_exists($path)) {
  199. $suffix = getSuffix($newfilename);
  200. switch ($suffix) {
  201. case 'bmp':
  202. $suffix = 'wbmp';
  203. break;
  204. case 'jpg':
  205. $suffix = 'jpeg';
  206. break;
  207. case 'png':
  208. case 'gif':
  209. case 'jpeg':
  210. break;
  211. default:
  212. pageError(405, gettext("Method Not Allowed"));
  213. exit();
  214. }
  215. //header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($imgfile)).' GMT');
  216. if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($imgfile)) {
  217. //header('HTTP/1.1 304 Not Modified');
  218. }
  219. header('Content-Type: image/'.$suffix);
  220. //header('Content-Length: ' . filesize($path));
  221. //@ob_clean();
  222. //@flush();
  223. readfile($path);
  224. }
  225. } else {
  226. echo "\n<p>Image: <img src=\"" . $path ."\" /></p>";
  227. }
  228.  
  229. ?>


W czym rzecz? Ten kod tworzy miniaturki w locie, ale... Dla pewnych zdarzeń zwraca je ( w FireBug są widoczne i w FF jak się da 'Odśwież obrazek' ), ale dla HTML nie, dlaczego tak się dzieje? Jak to można zmienić?

PS. Komentarze można usunąć.
Darti
Co znaczy "dla HTML nie" ?
Że w źródle strony nie widać zawartości obrazka, co jest raczej normalne (obrazek to kod binarny a nie tekstowy) ?
W kodzie powinno wypluć
Kod
<p>Image: <img src="nazwaplikuphp.php" /></p>
Largo
Cytat(Darti @ 7.09.2010, 15:09:21 ) *
Co znaczy "dla HTML nie" ?
Że w źródle strony nie widać zawartości obrazka, co jest raczej normalne (obrazek to kod binarny a nie tekstowy) ?
W kodzie powinno wypluć
Kod
<p>Image: <img src="nazwaplikuphp.php" /></p>


I tak robię, wywołuje i.php z odpowiednimi parametrami, ale podczas edycji miniaturki ( kadrowania ) problem się pojawia taki, że FireBug zwraca nową miniaturkę, ale w kodzie HTML jest jedynie link z alt, po najechaniu na element FireBug'iem i dany element <img> obrazek się pokazuje. Sam obrazek wypruwam w ten sposób:

  1. header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
  2. header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
  3. header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($imgfile)).' GMT');
  4. if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($imgfile)) {
  5. header('HTTP/1.1 304 Not Modified');
  6. }
  7. header('Content-Type: image/'.$suffix);
  8. @flush();
  9. readfile($path);


Wszystkie obrazki się przeładowują, dla wszystkich zwracane jest istnienie pliku ( nowej miniaturki ), ale czasem nie wyświetlają się mimo ich istnienia. Co może być powodem takiego zachowania? Przypomnę, że lista miniaturek jest generowana w 'locie' podczas stworzenia element <img> z odwołaniem do pliku PHP, który powinien je zwracać.

Pomożecie? Aktualny kod do zwracania miniaturki to:

  1. if(file_exists($path)) {
  2. $suffix = getSuffix($newfilename);
  3. $imageSize = filesize($path);
  4. switch ($suffix) {
  5. case 'bmp':
  6. $suffix = 'wbmp';
  7. break;
  8. case 'jpg':
  9. $suffix = 'jpeg';
  10. break;
  11. case 'png':
  12. case 'gif':
  13. case 'jpeg':
  14. break;
  15. default:
  16. pageError(405, gettext("Method Not Allowed"));
  17. exit();
  18. }
  19. $imageFunction = 'imagecreatefrom'.$suffix;
  20. if(function_exists($imageFunction)) {
  21. @$imageObject = $imageFunction($path);
  22. if(is_resource($imageObject)) {
  23. $imageShowFunction = 'image'.$suffix;
  24. if(function_exists($imageShowFunction)) {
  25. header('Content-Type: image/'.$suffix);
  26. header('Content-Length: '.$imageSize);
  27. $imageShowFunction($imageObject);
  28. imagedestroy($imageObject);
  29. unset($imageFunction);
  30. unset($imageObject);
  31. unset($imageShowFunction);
  32. }
  33. }
  34. }
  35. /*if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) == gmdate("D, d M Y H:i:s", $fmt).' GMT') {
  36. header('HTTP/1.0 304 Not Modified');
  37. header('Content-Length: 0');
  38. }*/
  39. }
fander
Chodzi ci o to że masz 100 obrazków w galerii i powiedzmy 20, 30, 40, 50, 66, 90, 95 się nie wyświetla mimo że w kodzie HTML masz ustawione
<img src="i.php?i=obrazek&w=200$h=100" alt="Nazwa obrazka" />
Jeśli tak to może serwer po prostu nie wyrabia z tworzeniem x miniaturek na raz.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.