Resize images with transparency in php

纵然是瞬间 提交于 2019-11-30 18:32:53
"They have not worked at all and I'm not sure why."

Well you must have been doing something wrong. The code from the linked duplicate with a couple of lines added to load and save the image:

$im = imagecreatefrompng(PATH_TO_ROOT."var/tmp/7Nsft.png");

$srcWidth = imagesx($im);
$srcHeight = imagesy($im);

$nWidth = intval($srcWidth / 4);
$nHeight = intval($srcHeight /4);

$newImg = imagecreatetruecolor($nWidth, $nHeight);
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight,
    $srcWidth, $srcHeight);

imagepng($newImg, PATH_TO_ROOT."var/tmp/newTest.png");

Produces the image:

i.e. this question (and answer) are a complete duplicate.

nana.chorage

i have used simpleImage class for resizing image. You can re-size your image with maintaining aspect ratio. this class is using imagecreatetruecolor and imagecopyresampled core Php functions to re-size image

  $new_image = imagecreatetruecolor($width, $height);
  imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->image = $new_image;

find complete code at http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!