我制作了一个PHP脚本,它使用目录中的所有图像动态创建照片库(在http://benxd.me/art/上)。它为每个图像生成缩略图,并为灯箱中的高质量图像添加水印。
但是,我遇到了一个问题:水印脚本和缩略图脚本都会逆时针旋转一个图像(http://benxd.me/assets/img/art/Advance.jpg)90度。
我有其他图片上传到目录,因此这个图片有问题导致它被PHP旋转吗?或者它是由我的脚本引起的?
以下是我的脚本供参考:
engine.php - 生成缩略图的脚本和图库页面上显示的HTML
<?php
/*========================================
http://www.techrepublic.com/article/create-a-dynamic-photo-gallery-with-php-in-three-steps/
http://stackoverflow.com/questions/38963849/php-exif-data-not-working
https://davidwalsh.name/generate-photo-gallery
========================================*/
/* function: generates thumbnail */
function make_thumb($src,$dest,$desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height*($desired_width/$width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
/* copy source image at a resized size */
imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest);
}
/* function: returns files from dir */
function get_files($images_dir,$exts = array('jpg')) {
$files = array();
if($handle = opendir($images_dir)) {
while(false !== ($file = readdir($handle))) {
$extension = strtolower(get_file_extension($file));
if($extension && in_array($extension,$exts)) {
$files[] = $file;
}
}
closedir($handle);
}
return $files;
}
/* function: returns a file's extension */
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
include("settings.php");
/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
}
}
// read EXIF headers
$exif = exif_read_data("$images_dir/$file", "FILE,COMPUTED,ANY_TAG,IFD0,THUMBNAIL,COMMENT,EXIF", true);
//generate HTML
echo '<li class="borderless img"><a href="image.php?n=',$file,'" data-lightbox="art" data-title="<b>',str_replace(".jpg", "", $file),'</b> (',$exif['IFD0']['Copyright'],') ',$exif['IFD0']['ImageDescription'],'"><img src="',$thumbnail_image,'" /></a></li>';
}
}
?>
image.php - 为灯箱使用的高质量图片添加水印
<?php
/*========================================
http://gazelleincorporated.com/dynamically-adding-a-watermark-to-an-image-using-php
========================================*/
include("settings.php");
//Let's say you sent the filename via the url, i.e. watermark.php?filename=myimage.jpg
$filename=$_REQUEST['n'];
//get the full path to the image:
$imgpath = $images_dir.$filename;
//OK cool, let's start the process of outputting the image with a watermark...
header('content-type: image/jpeg'); //HTTP header - assumes your images in the gallery are JPGs
//$watermarkfile is the filepath for your watermark image as a PNG-24 Transparent (ex: your logo)
$watermarkfile="../assets/img/copyright-trans.png";
//Get the attributes of the watermark file so you can manipulate it
$watermark = imagecreatefrompng($watermarkfile);
//Get the width and height of your watermark - we will use this to calculate where to put it on the image
list($watermark_width,$watermark_height) = getimagesize($watermarkfile);
//Now get the main gallery image (at $imgpath) so we can maniuplate it
$image = imagecreatefromjpeg($imgpath);
//Get the width and height of your image - we will use this to calculate where the watermark goes
$size = getimagesize($imgpath);
//Calculate where the watermark is positioned
//In this example, it is positioned in the lower right corner, 15px away from the bottom & right edges
$dest_x = ($size[0] - $watermark_width) / 2;
$dest_y = ($size[1] - $watermark_height) / 2;
//I used to use imagecopymerge to apply the watermark to the image
//However it does not preserve the transparency and quality of the watermark
//imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 70);
//So I now use this function which works beautifully:
//Refer here for documentation: http://www.php.net/manual/en/function.imagecopy.php
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
//Finalize the image:
imagejpeg($image);
//Destroy the image and the watermark handles
imagedestroy($image);
imagedestroy($watermark);
?>
的settings.php
<?php
$images_dir = '../assets/img/art/';
$thumbs_dir = '../assets/img/art/thumb/';
$thumbs_width = 325;
?>
答案 0 :(得分:0)
感谢@MarkSetchell,问题是由图片的exif方向标记引起的。我甚至都不知道存在这样的事情!我能够使用JPEG Autorotate删除exif方向标记。我的脚本现在以正确的方向显示图像。