简体   繁体   English

PHP创建缩略图,垂直图像更改为水平

[英]PHP creating thumbnails, vertical images changed to horizontal

Greetings...I'm resizing JPGs on the fly by directory. 问候...我正在按目录即时调整JPG的大小。 The photos are randomly horizontal or vertical in nature. 这些照片实际上是水平或垂直的。 My script (included below) works as expected - except in one specific case. 我的脚本(包含在下面)可以按预期工作-在一种特定情况下除外。 That case would be when I transfer photos directly from a memory card to the final directory, from which the thumbs are created. 就是这种情况,当我将照片从存储卡直接传输到最终目录时,即会从中创建缩略图。 In this case - my vertical photos generate horizontal thumbs. 在这种情况下-我的垂直照片会产生水平拇指。

If I process a folder of images that have been processed, resized or not, this problem doesn't happen. 如果我处理的是已处理,未调整大小的图像文件夹,则不会发生此问题。 The vertical photos are rotated on the camera, on the memory card and this is evidenced by the fact that on my local drive, verticals preview as verticals. 垂直照片在相机和存储卡上旋转,而以下事实证明了这一点:在我的本地驱动器上,垂直预览为垂直。 However if I run untouched files through the thumbnail script, it rotates verticals 90 degrees clockwise! 但是,如果我通过缩略图脚本运行未修改的文件,它将垂直方向顺时针旋转90度!

Does anyone smart out there have any ideas? 有没有聪明的人有什么主意? :) :)

Also I toyed with the idea of placing a 'rotate' button in my interface, but I can't get the php function rotateimage() to work. 我也玩过在界面中放置“旋转”按钮的想法,但是我无法使php函数rotateimage()工作。 Samples are simle...no errors but in browser I get a mile of symbols and text. 样本很简单……没有错误,但是在浏览器中我得到了一英里的符号和文本。 Seen that before? 以前看过吗?

Thanks. 谢谢。

    function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' ) 
    {
      echo "Creating thumbnail for {$fname} <br />";

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new tempopary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image 
      //imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }
  }
  // close the directory
  closedir( $dir );
}

Could it be possible that you're passing the height to your function instead of the width in your initial 3 variables? 您是否有可能将高度而不是初始3个变量的宽度传递给函数? That could create this problem. 那可能造成这个问题。 We don't have the piece of code that passes these variables, you might want to put it up. 我们没有传递这些变量的代码,您可能要提出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM