duanbi8529 2018-04-05 06:25
浏览 108

Yii2 imagecreatefrompng不适用于透明

I use custom model to crop and commpress my image file when uploading. The problem is when upload image/png ... it crop and compress the image, but transparent background replace with black background ... i can not see my mistake ... here is upload function in controller

$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles[' . $imgKey . ']'); // get the imageFiles
$pic = Yii::getAlias('@frontend/web') . '/product_photos/thumb-270/' . $model->getImageFolderName() . '/' . $fileName;  // set the thumb path
$pic2 = Yii::getAlias('@frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName; // set reale image path
$file->saveAs(Yii::getAlias('@frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName);
$image = file_get_contents(Yii::getAlias('@frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName);
file_put_contents($pic, $image);
$model->resizeImg($pic);
if($file->type!='image/png') {
    $settings->compress($pic, $pic, 90);
    $settings->compress($pic2, $pic2, 90);
}

here is a model function resizeimg:

 public function resizeImg($img) {
        $sz = getimagesize($img);
        $ratio = $sz[0] / $sz[1]; // w/h

        $w2 = Yii::$app->params['thumbswidth']; // thumb 1 width

        $image = new SimpleImage();

        $image->load($img);

        $image->resize($w2, round($w2 / $ratio));
        $image->save($img);
    }

and here is all model SimpleImage:

 var $image;
   var $image_type;

   function load($DATA, $FORMNAME = NULL) {

      $image_info = getimagesize($DATA);
      $this->image_type = $image_info[2];

      if( $this->image_type == IMAGETYPE_JPEG ) {

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

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

         $this->image = imagecreatefrompng($DATA);

      }
   }

   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $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 = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      imagesavealpha($new_image, true);
      $this->image = $new_image;
   }
  • 写回答

0条回答 默认 最新

    报告相同问题?