Автор оригинала: David Wong.
В этой статье описывается функция цвета фона обмена фотографиями сертификатов, реализованная с помощью PHP. Для вашей справки приведем следующие сведения:
php
//The background image and the original image need to keep the same width and height. The example original image here uses a blue background
init();
function init(){
$old = '1.png';
$new = '2.png';
//Create a PNG transparent graph
$img = imagecreatefrompng($old);
setpng($img,$old,$new);
}
function setpng($imgid,$filename,$savename){
$bg = ' bg.png '; // background image
$new = imagecreatefrompng($bg);//Create a PNG transparent graph
List ($width, $height) = getimagesize ($filename); // get the length and width
$white = imagecolorallocate ($imgid, 1155215); // select a replacement color. This is green
cleancolor($imgid,$white);
Imagecolortransparent ($imgid, $white); // replace the selected color with transparent
Imagecopymerge ($new, $imgid, 0,0,0,0, $width, $height, 100); // merge images
Imagepng ($new, $savename); // save the image
Imagedestroy ($imgid); // destroy
imagedestroy($new);
echo '
';
}
function cleancolor($imgid,$color){
$width = imagesx ($imgid); // get width
$height = image ($imgid); // get high
for($i=0;$i<$width;$i++){
for($k=0;$k<$height;$k++){
//Compare each pixel
$rgb = imagecolorat($imgid,$i,$k);
$r = ($RGB > > 16) & 0xff; // take R
$g = ($RGB > > 8) & 0xff; // take G
$B = $RGB & 0xff; // take B
$randr = 1.5;
$randg = 1;
$randb=1;
//The approximate position of the blue RGB. Replace with green
if($r<=65*$randr && $g<=225*$randg && $b<=255*$randb && $b*$randb>=100){
//If you can accurately calculate the position to be reserved, you can write absolute numbers here
if($i>=$width/2 && $i<=$width/2 && $k>=$height/2 && $k<=$height/2){
}else{
//Change the color
imagesetpixel($imgid,$i,$k,$color);
}
}
}
}
}
- $old относится к обрабатываемому изображению, указанному в формате PNG
- $new относится к имени изображения, выведенному после обработки
- $BG-это фоновое изображение
Более заинтересованные читатели, интересующиеся контентом, связанным с PHP, могут просмотреть специальные разделы этого веб-сайта: “Краткое изложение навыков работы с графикой и изображениями PHP”, “Навыки работы с массивом PHP (массив)”, “Учебник по структуре данных и алгоритмам PHP”, “Краткое изложение алгоритмов программирования PHP”, “Краткое изложение навыков математических операций PHP”, “Краткое изложение использования строк PHP (строк)” и “Краткое изложение навыков работы с общими базами данных PHP” “
Я надеюсь, что эта статья поможет вам в программировании на PHP.