1

私が作成したファイルアップロードスクリプトは、アップロード時にぼやけた画像をアップロードします!これは私の現在のスクリプトです。私が間違ったことを理解してみてください。スクリプトは画像を.pngとしてアップロードします。ユーザー名は、現在ログインしているユーザーの実際のユーザー名です。

元の画像は17x22であるため、ぼやけることはありません。

<?php
include('../class/resize.php');
//error_reporting(0);
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$path = "../files/cloaks/"; //set your folder path
$filename = $_FILES['photoimg']['tmp_name']; //get the temporary uploaded image name
$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg","GIF","JPG","PNG", "JPEG"); //add the formats you want to upload

        $name = $_FILES['photoimg']['name']; //get the name of the image
        $size = $_FILES['photoimg']['size']; //get the size of the image
        if(strlen($name)) //check if the file is selected or cancelled after pressing the browse button. 
        {
            list($txt, $ext) = explode(".", $name); //extract the name and extension of the image
            if(in_array($ext,$valid_formats)) //if the file is valid go on.
            {
            if($size < 2098888) // check if the file size is more than 2 mb
            {
            $actual_image_name =  $_POST['fname']; //actual image name going to store in your folder
            $tmp = $_FILES['photoimg']['tmp_name']; 
            if(move_uploaded_file($tmp, $path.$actual_image_name)) //check the path if it is fine
                {   
                    move_uploaded_file($tmp, $path.$actual_image_name); //move the file to the folder
                    $dburl = ('../files/cloaks/'.$actual_image_name.'');
                    $image = new ZiResize();
                    $image->load($dburl);
                    $image->resize(22,17);
                    $image->save($path.$actual_image_name);
                    //display the image after successfully upload
                    echo "<img src='files/cloaks/".$actual_image_name."'  class='preview'> <input type='hidden' name='actual_image_name' id='actual_image_name' value='$actual_image_name' />";

                }
            else
                {
                echo "failed";
                }
            }
            else
            {
                echo "Error! Max image size is 2 MB!";                  
            }
            }
            else
            {
                echo "Error! Invalid image format!";    
            }
        }
        else
        {       
        echo "Error! No file selected!";
        }       
    exit;
    }
?>

resize.phpコード

<?php
class ZiResize {

   var $image;
   var $image_type;

   function load($filename) {

      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {

         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {

         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {

         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image,$filename);
      }
      if( $permissions != null) {

         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image);
      }
   }
   function getWidth() {

      return imagesx($this->image);
   }
   function getHeight() {

      return imagesy($this->image);
   }
   function resizeToHeight($height) {

      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }

   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }

   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }

   function resize($width,$height) {
      $new_image = imagecreate($width, $height); 
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }      

}
?>
4

2 に答える 2

1

compression画像を保存するときに引数を変更する

function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null)

jpg形式を使用しています。phpでは、結果画像の品質を設定できます。その品質が元の画像よりも低い場合、サイズ変更されていなくても画像は「ぼやけた」ように見えます。

あなたはできる:

  1. 圧縮値を変更します($image->save($path.$actual_image_name, NULL, 100);
  2. 画像の形式を「圧縮」をサポートしていない他の形式に変更します

画像のサイズを変更していないため、次のように置き換えることができます。

$image->resize(22,17);
$image->save($path.$actual_image_name);

これとともに:

$image->save($path.$actual_image_name, NULL, 100);
于 2013-03-06T13:37:53.490 に答える
0

22pxx17pxにサイズ変更します。画像をそのサイズに縮小すると、常にぼやけて見えます。imagecreatetruecolorフルカラースペクトルが可能になるため、使用する必要もあります。imagecreateは色が制限されているため、画像がぼやけて見える可能性があります。

于 2013-03-06T13:28:26.290 に答える