脚本宝典收集整理的这篇文章主要介绍了php – 如何使用GD调整上传图像并将其转换为PNG,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
使用GD进行调整大小和PNG转换的最佳方式是什么?
编辑:可悲的是,只有GD可用在我需要使用的服务器上,没有ImageMagick.
<?PHP /* Resizes an image and converts IT to PNG returning the PNG data as a string */ function imageToPng($srCFile,$maxSize = 100) { list($width_orig,$height_orig,$type) = getimagesize($srcFile); // Get the aspect ratio $ratio_orig = $width_orig / $height_orig; $width = $maxSize; $height = $maxSize; // resize to height (orig is portrait) if ($ratio_orig < 1) { $width = $height * $ratio_orig; } // resize to width (orig is landscape) else { $height = $width / $ratio_orig; } // Temporarily increase the memory limit to allow for larger images ini_set('memory_limit','32M'); switch ($type) { case IMAGETYPE_GIF: $image = imagecreatefromgif($srcFile); break; case IMAGETYPE_JPEG: $image = imagecreateFromjpeg($srcFile); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($srcFile); break; default: throw new Exception('Unrecognized image type ' . $type); } // create a new blank image $newImage = imagecreatetruecolor($width,$height); // Copy the old image to the new image imagecopyresampled($newImage,$image,$width,$height,$width_orig,$height_orig); // Output to a temp file $destFile = tempnam(); imagepng($newImage,$destFile); // Free memory imagedestroy($newImage); if ( is_file($destFile) ) { $f = fopen($destFile,'rb'); $data = fread($f); fclose($f); // Remove the tempfile unlink($destFile); return $data; } throw new Exception('Image conversion Failed.'); }
以上是脚本宝典为你收集整理的php – 如何使用GD调整上传图像并将其转换为PNG全部内容,希望文章能够帮你解决php – 如何使用GD调整上传图像并将其转换为PNG所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。