1

I upload image with mime type png, and everything fine, file have transparent background

but begin I need change resize for this file like this

/home/ivan/host/olegroom/app/../web/uploads/photo/20161216022845_png-001.png

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

    /**
 * @param string $width
 * @param string $height
 * @param int $left
 * @param int $top
 */
public function resize($width, $height, $left = 0, $top = 0)
{
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, $left, $top, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}

and then I save my file

/**
 * @param string $filename
 * @param int $image_type
 * @param int $compression
 * @param null $permissions
 */
public function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 95, $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);
    }
}

and after resize I have black backgrounf but I don't need this, how to change resize for png mimetype ??

Soni Vimalkumar
  • 1,449
  • 15
  • 26
shuba.ivan
  • 3,824
  • 8
  • 49
  • 121
  • 1
    Not sure what library you are using, but check out this: http://stackoverflow.com/questions/279236/how-do-i-resize-pngs-with-transparency-in-php or this: http://stackoverflow.com/questions/13596794/resize-images-with-php-support-png-jpg – JustBaron Dec 16 '16 at 12:38

2 Answers2

1

You have to turn Off alpha blending, and save the original alpha channel data. Do this after the imagecopyresampled() call:

imagealphablending($new_image, false);
imagesavealpha($new_image, true);
Narf
  • 14,600
  • 3
  • 37
  • 66
0
$imageKind = array(
    'image/pjpeg',
    'image/jpeg',
    'image/JPG',

    'image/X-PNG',
    'image/PNG',
    'image/png',
    'image/x-png');
$WIDTH = 600;
$HEIGHT = 400;


$type = $_FILES['photo_' . $i]['type'];
if (in_array($type, $imageKind)) {
    $image = $_FILES['photo_' . $i]['tmp_name'];
    switch ($type) {
        case 'image/JPG':
        case 'image/pjpeg':
        case 'image/jpeg':
            $source = imagecreatefromjpeg($image);
            $extension = '.jpg';
            break;

        case 'image/X-PNG':
        case 'image/PNG' :
        case 'image/png':
        case 'image/x-png':
            $source = imagecreatefrompng($image);
            $extension = '.png';
            break;
    }

    $time = explode(" ", microtime());
    $time_name = ($time[1] + $time[0]) * 10000;

    $filename = ip2long($IP) . "_" . $time_name . "_" . $i . $extension;
    array_push($filename_array, $filename);
    array_push($fields, "photo_" . $i);
    array_push($values, $filename);


    list($width, $height) = getimagesize($image);
    $resize = imagecreatetruecolor($WIDTH, $HEIGHT);
    imagecopyresampled($resize, $source, 0, 0, 0, 0, $WIDTH, $HEIGHT, $width, $height);
    
    switch ($type) {
        case 'image/JPG':
        case 'image/pjpeg':
        case 'image/jpeg':
            imagejpeg($resize, $image);
            break;

        case 'image/X-PNG':
        case 'image/PNG' :
        case 'image/png':
        case 'image/x-png':
            imagealphablending($resize, false);
            imagesavealpha($resize, true);
            imagepng($resize, $image);
            break;
    }

    move_uploaded_file($image, "../upload/" . $filename);
seunggabi
  • 1,699
  • 12
  • 12