<small id='8hFQr'></small><noframes id='8hFQr'>

<i id='8hFQr'><tr id='8hFQr'><dt id='8hFQr'><q id='8hFQr'><span id='8hFQr'><b id='8hFQr'><form id='8hFQr'><ins id='8hFQr'></ins><ul id='8hFQr'></ul><sub id='8hFQr'></sub></form><legend id='8hFQr'></legend><bdo id='8hFQr'><pre id='8hFQr'><center id='8hFQr'></center></pre></bdo></b><th id='8hFQr'></th></span></q></dt></tr></i><div id='8hFQr'><tfoot id='8hFQr'></tfoot><dl id='8hFQr'><fieldset id='8hFQr'></fieldset></dl></div>

  • <legend id='8hFQr'><style id='8hFQr'><dir id='8hFQr'><q id='8hFQr'></q></dir></style></legend>
    1. <tfoot id='8hFQr'></tfoot>

        • <bdo id='8hFQr'></bdo><ul id='8hFQr'></ul>

      1. PHP临时文件上传无效图片资源

        时间:2023-12-01

            <bdo id='yzcB0'></bdo><ul id='yzcB0'></ul>

              <small id='yzcB0'></small><noframes id='yzcB0'>

                <tbody id='yzcB0'></tbody>
              <legend id='yzcB0'><style id='yzcB0'><dir id='yzcB0'><q id='yzcB0'></q></dir></style></legend>

              1. <tfoot id='yzcB0'></tfoot>
              2. <i id='yzcB0'><tr id='yzcB0'><dt id='yzcB0'><q id='yzcB0'><span id='yzcB0'><b id='yzcB0'><form id='yzcB0'><ins id='yzcB0'></ins><ul id='yzcB0'></ul><sub id='yzcB0'></sub></form><legend id='yzcB0'></legend><bdo id='yzcB0'><pre id='yzcB0'><center id='yzcB0'></center></pre></bdo></b><th id='yzcB0'></th></span></q></dt></tr></i><div id='yzcB0'><tfoot id='yzcB0'></tfoot><dl id='yzcB0'><fieldset id='yzcB0'></fieldset></dl></div>
                  本文介绍了PHP临时文件上传无效图片资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我有一个调整图像大小的类 (resizeManager.php):

                  Hi I have a class for resizing images (resizeManager.php):

                          Class ResizeManager
                      {
                          // *** Class variables
                          private $image;
                          private $width;
                          private $height;
                          private $imageResized;
                  
                          function __construct($fileName)
                          {
                              // *** Open up the file
                              $this->image = $this->openImage($fileName);
                  
                              // *** Get width and height
                              $this->width  = imagesx($this->image);
                              $this->height = imagesy($this->image);
                          }
                  
                          ## --------------------------------------------------------
                  
                          private function openImage($file)
                          {
                              // *** Get extension
                              $extension = strtolower(strrchr($file, '.'));
                  
                              switch($extension)
                              {
                                  case '.jpg':
                                  case '.jpeg':
                                      $img = @imagecreatefromjpeg($file);
                                      break;
                                  case '.gif':
                                      $img = @imagecreatefromgif($file);
                                      break;
                                  case '.png':
                                      $img = @imagecreatefrompng($file);
                                      break;
                                  default:
                                      $img = false;
                                      break;
                              }
                              return $img;
                          }
                  
                          ## --------------------------------------------------------
                  
                          public function resizeImage($newWidth, $newHeight, $option="auto")
                          {
                              // *** Get optimal width and height - based on $option
                              $optionArray = $this->getDimensions($newWidth, $newHeight, $option);
                  
                              $optimalWidth  = $optionArray['optimalWidth'];
                              $optimalHeight = $optionArray['optimalHeight'];
                  
                  
                              // *** Resample - create image canvas of x, y size
                              $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
                              imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
                  
                  
                              // *** if option is 'crop', then crop too
                              if ($option == 'crop') {
                                  $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
                              }
                          }
                  
                          ## --------------------------------------------------------
                  
                          private function getDimensions($newWidth, $newHeight, $option)
                          {
                  
                             switch ($option)
                              {
                                  case 'exact':
                                      $optimalWidth = $newWidth;
                                      $optimalHeight= $newHeight;
                                      break;
                                  case 'portrait':
                                      $optimalWidth = $this->getSizeByFixedHeight($newHeight);
                                      $optimalHeight= $newHeight;
                                      break;
                                  case 'landscape':
                                      $optimalWidth = $newWidth;
                                      $optimalHeight= $this->getSizeByFixedWidth($newWidth);
                                      break;
                                  case 'auto':
                                      $optionArray = $this->getSizeByAuto($newWidth, $newHeight);
                                      $optimalWidth = $optionArray['optimalWidth'];
                                      $optimalHeight = $optionArray['optimalHeight'];
                                      break;
                                  case 'crop':
                                      $optionArray = $this->getOptimalCrop($newWidth, $newHeight);
                                      $optimalWidth = $optionArray['optimalWidth'];
                                      $optimalHeight = $optionArray['optimalHeight'];
                                      break;
                              }
                              return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
                          }
                  
                          ## --------------------------------------------------------
                  
                          private function getSizeByFixedHeight($newHeight)
                          {
                              $ratio = $this->width / $this->height;
                              $newWidth = $newHeight * $ratio;
                              return $newWidth;
                          }
                  
                          private function getSizeByFixedWidth($newWidth)
                          {
                              $ratio = $this->height / $this->width;
                              $newHeight = $newWidth * $ratio;
                              return $newHeight;
                          }
                  
                          private function getSizeByAuto($newWidth, $newHeight)
                          {
                              if ($this->height < $this->width)
                              // *** Image to be resized is wider (landscape)
                              {
                                  $optimalWidth = $newWidth;
                                  $optimalHeight= $this->getSizeByFixedWidth($newWidth);
                              }
                              elseif ($this->height > $this->width)
                              // *** Image to be resized is taller (portrait)
                              {
                                  $optimalWidth = $this->getSizeByFixedHeight($newHeight);
                                  $optimalHeight= $newHeight;
                              }
                              else
                              // *** Image to be resizerd is a square
                              {
                                  if ($newHeight < $newWidth) {
                                      $optimalWidth = $newWidth;
                                      $optimalHeight= $this->getSizeByFixedWidth($newWidth);
                                  } else if ($newHeight > $newWidth) {
                                      $optimalWidth = $this->getSizeByFixedHeight($newHeight);
                                      $optimalHeight= $newHeight;
                                  } else {
                                      // *** Sqaure being resized to a square
                                      $optimalWidth = $newWidth;
                                      $optimalHeight= $newHeight;
                                  }
                              }
                  
                              return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
                          }
                  
                          ## --------------------------------------------------------
                  
                          private function getOptimalCrop($newWidth, $newHeight)
                          {
                  
                              $heightRatio = $this->height / $newHeight;
                              $widthRatio  = $this->width /  $newWidth;
                  
                              if ($heightRatio < $widthRatio) {
                                  $optimalRatio = $heightRatio;
                              } else {
                                  $optimalRatio = $widthRatio;
                              }
                  
                              $optimalHeight = $this->height / $optimalRatio;
                              $optimalWidth  = $this->width  / $optimalRatio;
                  
                              return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
                          }
                  
                          ## --------------------------------------------------------
                  
                          private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
                          {
                              // *** Find center - this will be used for the crop
                              $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
                              $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
                  
                              $crop = $this->imageResized;
                              //imagedestroy($this->imageResized);
                  
                              // *** Now crop from center to exact requested size
                              $this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
                              imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
                          }
                  
                          ## --------------------------------------------------------
                  
                          public function saveImage($savePath, $imageQuality="100")
                          {
                              // *** Get extension
                              $extension = strrchr($savePath, '.');
                              $extension = strtolower($extension);
                  
                              switch($extension)
                              {
                                  case '.jpg':
                                  case '.jpeg':
                                      if (imagetypes() & IMG_JPG) {
                                          imagejpeg($this->imageResized, $savePath, $imageQuality);
                                      }
                                      break;
                  
                                  case '.gif':
                                      if (imagetypes() & IMG_GIF) {
                                          imagegif($this->imageResized, $savePath);
                                      }
                                      break;
                  
                                  case '.png':
                                      // *** Scale quality from 0-100 to 0-9
                                      $scaleQuality = round(($imageQuality/100) * 9);
                  
                                      // *** Invert quality setting as 0 is best, not 9
                                      $invertScaleQuality = 9 - $scaleQuality;
                  
                                      if (imagetypes() & IMG_PNG) {
                                           imagepng($this->imageResized, $savePath, $invertScaleQuality);
                                      }
                                      break;
                  
                                  // ... etc
                  
                                  default:
                                      // *** No extension - No save.
                                      break;
                              }
                  
                              imagedestroy($this->imageResized);
                          }
                  
                  
                  
                  
                      }
                  

                  在我的页面 (mypage.php) 中,我想仅在其宽度>800 像素或高度>600 像素时调整我上传的图像的大小.

                  In my page (mypage.php) I want to resize my uploaded image only if its width>800px or height>600px.

                  所以我写了:

                  list($width, $height, $type, $attr) = getimagesize($_FILES['pictures']['tmp_name'][$number]); 
                  
                  if($width>800 || $height>600)
                  {
                   $resizemgr = new ResizeManager($_FILES['pictures']['tmp_name'][$number]);
                      //then the actual resizing                      
                      [...]
                  }
                  else
                  
                  $ret=@move_uploaded_file($_FILES['pictures']['tmp_name'][$number],$destinationFile); 
                  

                  但我收到这些错误

                  Warning: imagesx(): supplied argument is not a valid Image resource in resizeManager.php 
                  
                  Warning: imagesy(): supplied argument is not a valid Image resource in resizeManager.php 
                  

                  我可以这样指向一个临时文件吗?

                  Can I point to a temporary file this way?

                  谢谢

                  卢卡

                  推荐答案

                  下面的例子测试:

                  <form method="post" enctype="multipart/form-data" action="">
                  <input type="file" name="file" />
                  <div class="td" style="width: 100%; text-align: left;"><button type="submit">OK</button></div>
                  </form>
                  <?php
                  if( isset( $_FILES['file'] ) )
                  {
                      $img = file_get_contents( $_FILES['file']['tmp_name'] );
                      $image = imagecreatefromstring( $img );
                      echo imagesx( $image );
                  }
                  ?> 
                  

                  这篇关于PHP临时文件上传无效图片资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我如何知道使用 PHP 上传的文件数量? 下一篇:Twitter api 上传图片总是返回错误“媒体类型无法识别";

                  相关文章

                  <small id='6kMMZ'></small><noframes id='6kMMZ'>

                • <legend id='6kMMZ'><style id='6kMMZ'><dir id='6kMMZ'><q id='6kMMZ'></q></dir></style></legend>
                • <tfoot id='6kMMZ'></tfoot>

                  <i id='6kMMZ'><tr id='6kMMZ'><dt id='6kMMZ'><q id='6kMMZ'><span id='6kMMZ'><b id='6kMMZ'><form id='6kMMZ'><ins id='6kMMZ'></ins><ul id='6kMMZ'></ul><sub id='6kMMZ'></sub></form><legend id='6kMMZ'></legend><bdo id='6kMMZ'><pre id='6kMMZ'><center id='6kMMZ'></center></pre></bdo></b><th id='6kMMZ'></th></span></q></dt></tr></i><div id='6kMMZ'><tfoot id='6kMMZ'></tfoot><dl id='6kMMZ'><fieldset id='6kMMZ'></fieldset></dl></div>

                      • <bdo id='6kMMZ'></bdo><ul id='6kMMZ'></ul>