php说php制作的png无效

发布于 2024-10-01 16:02:55 字数 1182 浏览 3 评论 0原文

所以我有一个函数可以将 jpeg 图像转换为 png 图像,调整其大小然后保存它。然后稍后我回到它并在旋转函数中使用图像。但我不断收到错误。它说 uploads/image.png 不是有效的 PNG 文件。奇怪的是,它只对 php 编辑的 png 文件起作用。如果我删除 image.png 并从互联网上下载一个 png,名称为 image.png 就可以正常工作,只要我不通过第一个调整大小脚本运行它。

function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
    $imagecreated = imagecreatefromjpeg($filename);
    $this->image = $imagecreated;
    $extention = pathinfo($filename, PATHINFO_EXTENSION);
    $basename = basename($filename, ".".$extention);
    $newname = "uploads/".$basename;
    imagepng($imagecreated, $newname.".png", 0);
// ....???

function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height,
        $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}

然后我只用一个简单的方法保存文件,

imagepng(etc etc);

我进入上传文件夹,它看起来不错。它调整了大小和一切。我还注意到 Photoshop 也不会打开编辑后的 ​​png。

产生错误的代码行也在这里..

$image = imagecreatefrompng('uploads/image.png');

So I have a function that turns a jpeg into a png image resizes it then saves it. Then a bit later i come back to it and use the image in a rotate function. I keep getting errors though. It says uploads/image.png isnt a valid PNG file. The weird thing is that it only does then on php edited png files. If i delete image.png and download a png from the internet name is image.png is works fine as long as i dont run it through the first resize script.

function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
    $imagecreated = imagecreatefromjpeg($filename);
    $this->image = $imagecreated;
    $extention = pathinfo($filename, PATHINFO_EXTENSION);
    $basename = basename($filename, ".".$extention);
    $newname = "uploads/".$basename;
    imagepng($imagecreated, $newname.".png", 0);
// ....???

function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height,
        $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}

Then i save the file with just a simple

imagepng(etc etc);

I go to the uploads folder and it looks fine. its resized and everything. I also noticed photoshop wont open the edited png either.

Also the line of code that produces the error is here..


$image = imagecreatefrompng('uploads/image.png');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-10-08 16:02:55

可能的解决方法是为过滤器参数(imagepng 中的最后一个参数)添加 NULL

imagepng($image,$file_location,0,NULL);

我不能说这应该是需要的,但我读过一个类似的案例,其中有人指出他们必须这样做,可能取决于 libpng 或 gd lib 的版本。

Possible fix is to add a NULL for your filters parameter (the last parameter in imagepng)

ie.

imagepng($image,$file_location,0,NULL);

I can't say this should be needed, but I read a similar case where someone noted they had to do this, may be dependant on the version of libpng or gd lib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文