특정 조건이 충족되면 PHP 코드가 두 번 실행되는 이상한 문제가 있습니다. 내 코드는 JPEG 이미지에 워터 마크를 추가합니다 (필요한 경우 워터 마크를 빠르게 변경할 수 있도록 PHP에서 수행됨). 요청 된 경우 크기를 조정합니다. 요청을 빠르게하기 위해 처리 된 이미지가 캐시됩니다. 화상이 I가도 카운터를 증분있어로드 될 때마다, 다음과 같은 문제가 발생하지만 때 카운터가 2 씩 증가된다특정 조건에서 PHP 코드가 두 번 실행됩니다.
- 폭 또는 높이가 출력 MIME 타입이 설정되어 GET 요청
- (에 설정된
imagecreatefromjpeg()
가 호출되고 이미지 크기를 조정 예상대로 스크립트
- 브라우저. Firefox는 Internet Explorer 11이 정상적으로 작동하고 액세스 로그로 확인됩니다 (I h aven't 다른 브라우저를 테스트)
header()
호출을 실행 주석 -.이 라인은 스크립트가 정상적으로 실행 주석되는 스크립트는 이미지 크기가 조정되지 않은 일반적 경우 실행
여기 내 코드입니다 :.
function incrementPhotoViewCount($photoId) {
$photoId = intval($photoId);
if (!($stmt = $GLOBALS['mysqli']->prepare("UPDATE `Photo` SET `view_count` = `view_count` + 1 WHERE `photo_id` = ?"))) {
error_log("Prepare failed: (" . $GLOBALS['mysqli']->errno . ") " . $GLOBALS['mysqli']->error);
return false;
}
if (!$stmt->bind_param("i", $photoId)) {
error_log("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
return false;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
return false;
}
}
여기 무슨 일이야 :
<?php
require_once("../config.php");
if (empty($_GET['photoid'])) {
header("Location: /photos");
die();
}
$photoId = intval($_GET['photoid']);
$photo = getPhotoData($photoId);
if ($photo == null) {
http_response_code(404);
echo "That photo doesn't exist.";
} else {
$filename = $photo['photo_filename'];
$largePath = "large/";
$cachePath = $largePath . "view_cache/";
$cache = false;
if (!isset($_GET['admin']) && !isset($_GET['thumb']) && !isset($_GET['tile'])) {
incrementPhotoViewCount($photoId);
}
if (isset($_GET['height'])) {
list($originalWidth, $originalHeight) = getimagesize($largePath . $photo['photo_filename']);
$height = min(intval($_GET['height']), $originalHeight);
if (file_exists($cachePath . $height . "h" . $filename)) {
$im = imagecreatefromjpeg($cachePath . $height . "h" . $filename);
$cache = true;
} else {
$im = resizeImage($largePath . $photo['photo_filename'], $originalWidth, $height);
}
} else if (isset($_GET['width'])) {
list($originalWidth, $originalHeight) = getimagesize($largePath . $photo['photo_filename']);
$width = min(intval($_GET['width']), $originalWidth);
if (file_exists($cachePath . $width . "w" . $filename)) {
$im = imagecreatefromjpeg($cachePath . $width . "w" . $filename);
$cache = true;
} else {
$im = resizeImage($largePath . $photo['photo_filename'], $width, $originalHeight);
}
} else {
if (file_exists($cachePath . $filename)) {
$im = imagecreatefromjpeg($cachePath . $filename);
$cache = true;
} else {
$im = imagecreatefromjpeg($largePath . $photo['photo_filename']);
}
}
if (!$cache && ((!isset($height) || $height > 400) && (!isset($width) || $width > 350))) {
$watermark = imagecreatefrompng("../images/watermark.png");
//Preserve original watermark transparency
imagealphablending($watermark, true); // setting alpha blending on
imagesavealpha($watermark, true); // save alphablending setting (important)
//Set the margins for the watermark and get the height/width of the stamp image
$marginLeft = 20;
$marginBottom = 20;
$sx = imagesx($watermark);
$sy = imagesy($watermark);
//Merge the stamp onto our photo
imagecopy($im, $watermark, $marginLeft, imagesy($im) - $sy - $marginBottom, 0, 0, $sx, $sy);
}
//Output the image and free memory
header("Content-Type: image/jpeg");
imagejpeg($im);
if (!$cache) {
if (isset($_GET['height'])) {
imagejpeg($im, $cachePath . $height . "h" . $filename);
} else if (isset($_GET['width'])) {
imagejpeg($im, $cachePath . $width . "w" . $filename);
} else {
imagejpeg($im, $cachePath . $filename);
}
}
imagedestroy($im);
}
function resizeImage($file, $w, $h, $crop = false) {
list($width, $height) = getimagesize($file);
$r = $width/$height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
다음 뷰 카운터를 업데이트하는 코드가 있어요?
CSS/JS 문제가 있음을 알기 때문에 '너무 광범위하게'표시 : 서버 측, PHP, 대부분의 브라우저에서 출력이 동일합니다. . 관련성이있는 부분에 코드를 압축하고 생성 된 결함있는 HTML을 추가 할 수 있습니까? cURL과 같은 도구로 재현 할 수 있습니까? 너는 무엇을 이미 시도 했는가? – berkes
''요청에 대해서도 페이지 뷰를 세고있는 것으로 보입니다. 페이지에 단일 이미지를 유지하고 카운트를 업데이트하지 않는지 확인하십시오. – Saqueib
죄송합니다. 문제를 테스트하기 위해 스크립트를 직접로드하고 있습니다 (즉, ''태그 제외). – pcuser42