如何在使用PHP调整大小时在PNG上保持透明背景? [英] How to keep transparent background on PNG when resizing with PHP?

查看:207
本文介绍了如何在使用PHP调整大小时在PNG上保持透明背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用PHP调整图片大小时遇到​​问题,尤其是使用透明背景的PNG文件,而是保持透明背景,它变成黑色背景。如何解决此问题?



这是调整大小的脚本:

 <?php 
class resize {

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 = 85,$ 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);
}
}
函式输出($ image_type = IMAGETYPE_JPEG){

if($ image_type == IMAGETYPE_JPEG){
imagejpeg($ this-> ;图片);
} 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());
$ this-> image = $ new_image;
}

}
?>

这就是我所说的:

  $ picture_directory =images /。 $ _FILES [file] [name]; 

include('arqinc / resizing.php');
$ image = new resize();
$ image-> load($ picture_directory);
$ image-> resize(660,780);
$ image-> save($ picture_directory);

编辑



ive将我的调整大小功能更改为:

  function resize($ width,$ height){

$ new_image = imagecreatetruecolor($ width,$ height);
$ transparent = imagefill($ new_image,0,0,imagecolorallocatealpha($ new_image,255,255,255,127));
imagecopyresampled($ new_image,$ this-> image,0,0,0,0,$ width,$ height,$ this-> getWidth(),$ this-> getHeight());
$ this-> image = $ new_image;
}

但现在它保持背景白色,不透明。



编辑2:



解决了这个调整大小功能的问题, p>

http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/



感谢
Tahir Yasin 链接它:D

解决方案

我发现另一篇有相同问题的帖子,根据后面的代码可以用来保存透明度。

  imagealphablending($ targetImage,false); 
imagesavealpha($ targetImage,true);

参考:

请投票,如果它有助于你。 :)


I'm having a issue when resizing pictures with PHP, especially with PNG files with transparent backgrounds, instead keeping the transparent background, it turns into a black background. How can I fix this?

This is the script for resizing:

<?php         
class resize{

   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=85, $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());
      $this->image = $new_image;
   }      

}
?>

And this is how I call it:

$picture_directory="images/" . $_FILES["file"]["name"];

    include('arqinc/resizing.php');
    $image = new resize();
    $image->load($picture_directory);
    $image->resize(660,780);
    $image->save($picture_directory);

EDIT:

ive changed my resize function to this:

function resize($width,$height) {

      $new_image = imagecreatetruecolor($width, $height);
      $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127));
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }  

But now it keeps the background white, not transparent.

EDIT 2:

Solved, this resize function is broken, this one works flawlessly:

http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/

Thanks to Tahir Yasin for linking it :D

解决方案

I found another post with same question, according to that post following code can be used to preserve transparancy.

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

Reference: Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

Please vote-up if it helps you. :)

这篇关于如何在使用PHP调整大小时在PNG上保持透明背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆