繁体   English   中英

我如何在 PHP 中使用 imagescale 调整图像大小

[英]How i can resize Images with imagescale in PHP

我尝试使用以下方法调整图像大小

代码

$thumbimg = $img->resizeImage($thumb, 200, 200);

public function resizeImage($imagePath,$new_width,$new_height) {

            $fileName = pathinfo($imagePath,PATHINFO_FILENAME);
            $ext = pathinfo($imagePath,PATHINFO_EXTENSION);
            $fullPath = pathinfo($imagePath,PATHINFO_DIRNAME)."/".$fileName.'.'.$ext;
            if (file_exists($fullPath)) {
                return $fullPath;
            }
            $image = $this->openImage($imagePath);
            if ($image == false) {
                return null;
            }
            $width = imagesx($image);
            $height = imagesy($image);
            $imageResized = imagecreatetruecolor($width, $height);

            if ($imageResized == false) {
                return null;
            }
            $image = imagecreatetruecolor($width , $height);

            $imageResized = imagescale($image,$new_width,$new_height);
            touch($fullPath);
            $write = imagepng($imageResized,$fullPath);
            if (!$write) {
                imagedestroy($imageResized);    
                return null;
            }
            imagedestroy($imageResized);
            return $fullPath;
        }

实际它以原始宽度和高度保存我的图像。 但为什么? 怎么了?

如果$imagePath引用现有图像,则$fullPath将引用同一个文件。 所以要么文件存在,在这种情况下它被返回,要么文件不存在,在这种情况下你不能打开图像并返回null 无论哪种方式,都不使用图像调整大小代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM