首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在PHP中保存上传图像文件的方形缩略图

在PHP中保存上传图像文件的方形缩略图
EN

Stack Overflow用户
提问于 2016-02-21 06:58:30
回答 1查看 278关注 0票数 0

我正在尝试用php创建并保存一个上传图像文件的正方形版本(thumb)。我当前的脚本像它应该裁剪的那样,但图像是完全黑色的。下面是我的代码:

代码语言:javascript
复制
if ($_FILES['profile_pic']['type'] == "image/png") {
                            $is_image = true;
                            $newImage = imagecreatefrompng($img);
                        } else if ($_FILES['profile_pic']['type'] == "image/jpeg") {
                            $is_image = true;
                            $newImage = imagecreatefromjpeg($img);
                        } else if ($_FILES['profile_pic']['type'] == "image/gif") {
                            $is_image = true;
                            $newImage = imagecreatefromgif($img);
                        } else {
                            $is_image = false;
                        }
                        $img         = $_FILES["profile_pic"]['tmp_name'];
                        $min_width   = 100;
                        $min_height  = 100;
                        $width       = 0;
                        $height      = 0;
                        if ($is_image) {
                            list($width, $height) = getimagesize($img);
                        }

                        if ($is_image && $height >= $min_height && $width >= $min_width) {
                            $img         = $_FILES["profile_pic"]['tmp_name'];
                            $imgPath     = "../img/profile_pics/{$member_id}.png";

                            // Resize
                            $aspect_ratio = $width / $min_width;
                            $new_height = $height * $aspect_ratio;
                            $canvas1 = imagecreatetruecolor($min_width, $new_height);
                            imagecopyresampled($canvas1, $newImage, 0, 0, 0, 0, $min_width, $new_height, $width, $new_height);

                            // Crop
                            $canvas2 = imagecreatetruecolor($min_width, $min_height);
                            imagecopyresampled($canvas2, $newImage, 0, 0, 0, 0, $min_width, $min_height, $min_width, $min_height);
                            imagejpeg($canvas2, $imgPath, 80);
                            imagedestroy($canvas1);

                        }

我意识到以前有人问过这个问题,但由于某些原因,我似乎不能让我自己的脚本工作。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35530147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档