I have a landscape PNG image which I want to convert to a square image but with the whole image visable (in css-terms: contain).
I have managed that, and the transparency behind the image is preserved, but above and below the image it is black. How do I manage to get that transparent too?
move_uploaded_file($uploadedFile,$targetFile);
$image = ($fileExtension == 'png') ? imagecreatefrompng($targetFile) : imagecreatefromjpeg($targetFile);
unlink($targetFile);
$filename = $targetFile;
$width = imagesx($image);
$height = imagesy($image);
$thumb_width = 400;
$thumb_height = 400;
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
$dst_image = $thumb;
$src_image = $image;
$dst_w = $thumb_width;
$dst_h = $thumb_height;
$y = $width > $height ? ($width-$height)/2 : 0;
$x = $width > $height ? 0 : ($height-$width)/2;
$size = $width > $height ? $width : $height;
$src_w = $size;
$src_h = $size;
$src_x = -$x;
$src_y = -$y;
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagealphablending($image, false);
imagesavealpha($image, true);
imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
$blobName = 'icon.png';
putBlobImage($storageClient,$storageContainer,$thumb,$blobName,$fileExtension);
UPDATE
I did manage to remove the black boxes, but now also the black from my image disappeared (most of it though). How do I preserve the black from the original picture? See example and code below.
move_uploaded_file($uploadedFile,$targetFile);
$image = ($fileExtension == 'png') ? imagecreatefrompng($targetFile) : imagecreatefromjpeg($targetFile);
unlink($targetFile);
$filename = $targetFile;
$width = imagesx($image);
$height = imagesy($image);
$thumb_width = 400;
$thumb_height = 400;
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
$black = imagecolorallocate($thumb, 0, 0, 0);
imagecolortransparent($thumb,$black);
$dst_image = $thumb;
$src_image = $image;
$sc = $width/$thumb_width;
$dst_w = $width/$sc;
$dst_h = $height/$sc;
$y = $width > $height ? (400-$dst_h)/2 : 0;
$x = $width > $height ? 0 : (400-$dst_w)/2;
$size = $width > $height ? $width : $height;
$src_w = $width;
$src_h = $height;
$position = array(0,0,$x,$y);
list($src_x,$src_y,$dst_x,$dst_y) = $position;
//imagealphablending($src_image, false);
//imagesavealpha($src_image, true);
//imagealphablending($dst_image, true);
//imagesavealpha($dst_image, true);
imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
$blobName = 'icon.png';
putBlobImage($storageClient,$storageContainer,$thumb,$blobName,$fileExtension);
$data = $blobName.'@@'.$uploadedFileName.'@@'.$fileExtension;
$return .= $data;
I finally managed to fix it!
move_uploaded_file($uploadedFile,$targetFile);
$image = ($fileExtension == 'png') ? imagecreatefrompng($targetFile) : imagecreatefromjpeg($targetFile);
unlink($targetFile);
$filename = $targetFile;
$width = imagesx($image);
$height = imagesy($image);
$thumb_width = 400;
$thumb_height = 400;
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
$color = imagecolorallocatealpha($thumb, 255, 0, 0, 127);
imagefill($thumb, 0, 0, $color);
// THE LINE ABOVE DID THE TRICK
imagecolortransparent($thumb,$color);
$dst_image = $thumb;
$src_image = $image;
$sc = $width/$thumb_width;
$dst_w = $width/$sc;
$dst_h = $height/$sc;
$y = $width > $height ? (400-$dst_h)/2 : 0;
$x = $width > $height ? 0 : (400-$dst_w)/2;
$size = $width > $height ? $width : $height;
$src_w = $width;
$src_h = $height;
$position = array(0,0,$x,$y);
list($src_x,$src_y,$dst_x,$dst_y) = $position;
//imagealphablending($src_image, false);
//imagesavealpha($src_image, true);
//imagealphablending($dst_image, false);
//imagesavealpha($dst_image, true);
imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
$blobName = 'icon.png';
putBlobImage($storageClient,$storageContainer,$thumb,$blobName,$fileExtension);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.