Ok so I needed to resize with transparency and I see how this question's answer and my question's answer are similar but not exactly the same so I shall post my answer.
I want to point out that I modified the answer from HERE to answer my personal need and the OP's question. (Which was about keeping transparency of the 1 and only image that will be copied/cloned/or resized)
In Addition: The transparency color will stay with that resource and apply it to other transparent images copied onto it. The workaround I used was that I used imagecopy() to copy the cloned file in my example to clear the allocated transparency color.
$im = imagecreatefromstring(file_get_contents('I/image.png')); //
$imw = imagesx($im); /*w*/ $imh = imagesy($im); /*h*/
$tmpIm = imagecreatetruecolor($imw, $imh); // This is black by default
imagealphablending($tmpIm, false); imagesavealpha($tmpIm, true); // Set these as usual
$wht = imagecolorallocatealpha($tmpIm, 255, 255, 255, 127); // white
imagecolortransparent($tmpIm, $wht); // Setting this seems to be unsettable and will make this color transparent on all images later coppied to it
imagealphablending($tmpIm, false); imagesavealpha($tmpIm, true); // Set these as usual
// That is it... As long as you set the transparency of the background before you copy it should work. That means now at least some of the other answers can work now.
imagecopyresampled($tmpIm, $im, 0, 0, 0, 0, $imw, $imh, $imw, $imh); // Copy or resize whatever floats your boat. The above process is what does the trick.
$newIm = imagecreatetruecolor($imw, $imh); // Begin Clone
imagealphablending($newIm, false); imagesavealpha($newIm, true); // Set these as usual
imagecopyresampled($newIm, $tmpIm, 0, 0, 0, 0, $imw, $imh, $imw, $imh); // Clearing the tranparent color previously set (Workaround)
imagedestroy($tmpIm);
header('Content-Type: image/png');
imagepng($newIm); // see I told you.. Or he did... Whatever, it works...
So for the function, it would go:
function F_img_clone($im,$trgt,$id){
$imw = imagesx($im); $imh = imagesy($im); // Height and Width of Original
if(empty($trgt) === true){
$rsiw = $imw; $rsih = $imh; // if there are no H or W changes
}else{ // if there are H or W changes
if( $imw > $imh ) {
$pcntg = ( ($trgt??1920) / $imw );
}else{
$pcntg = ( ($trgt??1920) / $imh );
}
$rsiw = round($imw * $pcntg); $rsih = round($imh * $pcntg);
}
$ni = imagecreatetruecolor($rsiw, $rsih);// Begin the background
imagealphablending($ni, false); imagesavealpha($ni, true); // To keep the alpha channel
$wht = imagecolorallocatealpha($ni, 255, 255, 255, 127); // white
imagecolortransparent($ni, $wht); // Setting this seems to be unsettable and will make this color transparent on all images later coppied to it
imagealphablending($ni, false); imagesavealpha($ni, true);
imagecopyresampled($ni, $im, 0, 0, 0, 0, $rsiw, $rsih, $imw, $imh);
imagealphablending($ni, true); imagesavealpha($ni, false);
imagedestroy($im);
$imw = imagesx($ni); /*h*/ $imh = imagesy($ni); /*w*/
${$id} = imagecreatetruecolor($imw, $imh); // Begin Clone
imagealphablending(${$id}, false); imagesavealpha(${$id}, true); // Set these as usual
imagecopyresampled(${$id}, $ni, 0, 0, 0, 0, $imw, $imh, $imw, $imh); // Clearing the tranparent color previously set (Workaround)
imagedestroy($ni);
return ${$id};
}
So use it as:
$image = imagecreatefromstring(file_get_contents('I/image.png')); //
$clone = F_img_clone($image,'','clone');
The original $image will be intact.