-
慕斯王
这样做可以将PNG安全地转换为白色透明的JPG。$image = imagecreatefrompng($filePath);$bg = imagecreatetruecolor(imagesx($image), imagesy($image));imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));imagealphablending($bg, TRUE);imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));imagedestroy($image);$quality = 50; // 0 = worst / smaller file, 100 = better / bigger file imagejpeg($bg, $filePath . ".jpg", $quality);imagedestroy($bg);
-
慕少森
请注意您要转换的内容。JPG不支持Alpha透明度,而PNG则支持。您将丢失该信息。要进行转换,您可以使用以下功能:// Quality is a number between 0 (best compression) and 100 (best quality)function png2jpg($originalFile, $outputFile, $quality) { $image = imagecreatefrompng($originalFile); imagejpeg($image, $outputFile, $quality); imagedestroy($image);}此函数使用GD库中的imagecreatefrompng()和imagejpeg()函数。
-
繁华开满天机
<?phpfunction createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth) { $explode = explode(".", $imageName); $filetype = $explode[1]; if ($filetype == 'jpg') { $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName"); } else if ($filetype == 'jpeg') { $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName"); } else if ($filetype == 'png') { $srcImg = imagecreatefrompng("$imageDirectory/$imageName"); } else if ($filetype == 'gif') { $srcImg = imagecreatefromgif("$imageDirectory/$imageName"); } $origWidth = imagesx($srcImg); $origHeight = imagesy($srcImg); $ratio = $origWidth / $thumbWidth; $thumbHeight = $origHeight / $ratio; $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight); if ($filetype == 'jpg') { imagejpeg($thumbImg, "$thumbDirectory/$imageName"); } else if ($filetype == 'jpeg') { imagejpeg($thumbImg, "$thumbDirectory/$imageName"); } else if ($filetype == 'png') { imagepng($thumbImg, "$thumbDirectory/$imageName"); } else if ($filetype == 'gif') { imagegif($thumbImg, "$thumbDirectory/$imageName"); }} ?>这是一个非常好的缩略图脚本=)这是一个示例:$ path =原始图片所在文件夹的路径。$ name =要为其制作缩略图的文件的文件名。$ thumbpath =要将缩略图保存到的目录的路径。$ maxwidth = PX中缩略图的最大宽度,例如 100(将为100px)。createThumbnail($path, $name, $thumbpath, $maxwidth);