PHP code running twice under certain conditions -
i'm having unusual issue when conditions met php code run twice. code adds watermark jpeg image (done in php allow me alter watermark if necessary), , resized if requested. speed requests processed images cached. whenever image loaded i'm incrementing view counter, when next happens counter increased 2:
width or height set in request the output mime type set (commenting outheader() phone call runs script expected imagecreatefromjpeg() called , image resized - when these lines commented out script runs normally. script runs if image not resized. the browser firefox. net explorer 11 behaves normally, confirmed access logs. (i haven't tested other browsers.) here's code:
<?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 margins watermark , height/width of stamp image $marginleft = 20; $marginbottom = 20; $sx = imagesx($watermark); $sy = imagesy($watermark); //merge stamp onto our photo imagecopy($im, $watermark, $marginleft, imagesy($im) - $sy - $marginbottom, 0, 0, $sx, $sy); } //output image , 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); homecoming $dst; } here's code updates view counter:
function incrementphotoviewcount($photoid) { $photoid = intval($photoid); if (!($stmt = $globals['mysqli']->prepare("update `photo` set `view_count` = `view_count` + 1 `photo_id` = ?"))) { error_log("prepare failed: (" . $globals['mysqli']->errno . ") " . $globals['mysqli']->error); homecoming false; } if (!$stmt->bind_param("i", $photoid)) { error_log("binding parameters failed: (" . $stmt->errno . ") " . $stmt->error); homecoming false; } if (!$stmt->execute()) { echo "execute failed: (" . $stmt->errno . ") " . $stmt->error; homecoming false; } } what's going on here?
some late-night googling answered question - turns out firefox has bug causes load dynamically generated images twice when loaded directly. it's fine when loaded through <img> tag, , other browsers behave normally. @ else having issue :)
php
No comments:
Post a Comment