Очень нужно чтобы у картинки белый фон таким же и остался, а не менялся на черный. Помогите пож, вот весь код выкладываю:
class SimpleImage {
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 = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
$image = new SimpleImage();
$image->load('img/1.png');
$image->resizeToWidth(250);
$image->save('img/2.png');
echo "<img src='img/2.png'>";
(Добавление)
Да и с картинками jpg фон остается прежним, а почему в png меняется на черный?
1. Sergey8990 - 25 Декабря, 2011 - 02:57:40 - перейти к сообщению
2. Panoptik - 25 Декабря, 2011 - 03:16:18 - перейти к сообщению
я так подозревая что черный фон появляется у картинок png и возможно у gif. то есть у тех у которых поддерживается альфа-канал. тут могу посоветовать заменить при обработке этих форматов функцию imagecreatetruecolor на imagecreate
а фон там тоже можно добавлять. прост нужно параметры посмотреть. вобщемс попробуйте, а там видно будет.
пысы: в моем варианте альфа канал остается на месте, то есть останется прозрачным где и был изначально.
и еще код нужно обрамлять в специальные теги. будет удобнее читать и вам и остальным
а фон там тоже можно добавлять. прост нужно параметры посмотреть. вобщемс попробуйте, а там видно будет.
пысы: в моем варианте альфа канал остается на месте, то есть останется прозрачным где и был изначально.
и еще код нужно обрамлять в специальные теги. будет удобнее читать и вам и остальным
3. Striker - 09 Марта, 2013 - 23:38:12 - перейти к сообщению
Конечно для автора уже не актуально, но может кому-то другому пригодиться:
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- <?PHP
- class SimpleImage {
- public $image;
- public $image_type;
- function load($filename) {
- $this->image_type = $image_info[2];
- if( $this->image_type == IMAGETYPE_JPEG ) {
- } elseif( $this->image_type == IMAGETYPE_GIF ) {
- } elseif( $this->image_type == IMAGETYPE_PNG ) {
- }
- return $image_info;
- }
- function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
- // do this or they'll all go to jpeg
- $image_type=$this->image_type;
- if( $image_type == IMAGETYPE_JPEG ) {
- } elseif( $image_type == IMAGETYPE_GIF ) {
- } elseif( $image_type == IMAGETYPE_PNG ) {
- // need this for transparent png to work
- }
- if( $permissions != null) {
- }
- }
- function output($image_type=IMAGETYPE_JPEG) {
- if( $image_type == IMAGETYPE_JPEG ) {
- } elseif( $image_type == IMAGETYPE_GIF ) {
- } elseif( $image_type == IMAGETYPE_PNG ) {
- }
- }
- function getWidth() {
- }
- function getHeight() {
- }
- 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) {
- if( $this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG ) {
- if($current_transparent != -1) {
- $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
- } elseif( $this->image_type == IMAGETYPE_PNG) {
- }
- }
- imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
- $this->image = $new_image;
- }
- }
- ?>