2017-02-25 48 views
1

我做了一個PHP腳本,它使用目錄中的所有圖像動態地創建照片庫(在http://benxd.me/art/上)。它爲每個圖像生成縮略圖,併爲lightbox中的水印使用高質量圖像。爲什麼PHP腳本翻轉圖像?

但是,我有一個問題:水印腳本和縮略圖腳本逆時針旋轉一個圖像(http://benxd.me/assets/img/art/Advance.jpg)90度。

我有其他圖片上傳到目錄,所以有這個圖像的問題,導致它被PHP旋轉?或者是否會由我的腳本引起?

這裏是我的參考腳本:

engine.php - 生成縮略圖和畫廊網頁

<?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上顯示的HTML腳本 - 水印的通過燈箱使用的高品質的圖像

<?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; 
?> 
+1

該文件的方向在其元數據中被設置爲'Right Top'a.k.a.'旋轉90'。我想PHP正在履行這一點。 –

回答

0

感謝@MarkSetchell,這個問題是由圖像的Exif方向標記引起的。我甚至不知道這樣的事情存在!我能夠使用JPEG Autorotate刪除exif方向標籤。我的腳本現在以正確的方向顯示圖像。