PHP映像上传-从一种类型转换为多种类型

lian

目前,我可以上传jpg文件,并将它们另存为jpg文件。显然我无法上传/保存png文件,因为它很难设置为jpg。我正在尝试找出如何从jpg移到仅允许png,jpg和jpeg的方式。

public static function createAvatar()
{
    // check if upload fits all rules
    AvatarModel::validateImageFile();

    // create a jpg file in the avatar folder, write marker to database
    $target_file_path = Config::get('PATH_AVATARS') . Session::get('user_id');
    AvatarModel::resizeAvatarImage($_FILES['avatar_file']['tmp_name'], $target_file_path, Config::get('AVATAR_SIZE'), Config::get('AVATAR_SIZE'), Config::get('AVATAR_JPEG_QUALITY'));
    AvatarModel::writeAvatarToDatabase(Session::get('user_id'));
    Session::set('user_avatar_file', AvatarModel::getPublicUserAvatarFilePathByUserId(Session::get('user_id')));
    Session::add('feedback_positive', Text::get('FEEDBACK_AVATAR_UPLOAD_SUCCESSFUL'));
    return true;
}

public static function resizeAvatarImage($source_image, $destination, $final_width = 130, $final_height = 130, $quality = 8)
{
    list($width, $height) = getimagesize($source_image);

    if (!$width || !$height) {
        return false;
    }

    //saving the image into memory (for manipulation with GD Library)
    $myImage = imagecreatefromjpeg($source_image);

    // calculating the part of the image to use for thumbnail
    if ($width > $height) {
        $y = 0;
        $x = ($width - $height) / 2;
        $smallestSide = $height;
    } else {
        $x = 0;
        $y = ($height - $width) / 2;
        $smallestSide = $width;
    }

    // copying the part into thumbnail, maybe edit this for square avatars
    $thumb = imagecreatetruecolor($final_width, $final_height);
    imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $final_width, $final_height, $smallestSide, $smallestSide);

    // add '.jpg' to file path, save it as a .jpg file with our $destination_filename parameter
    $destination .= '.jpg';
    imagejpeg($thumb, $destination, $quality);

    // delete "working copy"
    imagedestroy($thumb);

    if (file_exists($destination)) {
        return true;
    }
    // default return
    return false;
}

public static function writeAvatarToDatabase($user_id)
{
    $database = DatabaseFactory::getFactory()->getConnection();

    $query = $database->prepare("UPDATE users SET user_has_avatar = TRUE WHERE user_id = :user_id LIMIT 1");
    $query->execute(array(':user_id' => $user_id));
}

这是问题所在

    $destination .= '.jpg';
    imagejpeg($thumb, $destination, $quality);

我试过在文件类型上添加一个开关,然后根据文件具有的文件类型进行imagejpeg / png / jpg(,,,)的处理,但似乎无法通过.tmp文件,因此该文件不起作用。

有任何想法吗?

拉斯克拉特

您将需要从头开始将图像创建为预期文件。这是我使用的课程,然后添加到您的课程中。您可以从一个类复制到另一个类,但是您至少可以看到需要更改的地方:

class   AvatarModel
    {
        public static function resizeAvatarImage($source_image, $destination, $final_width = 130, $final_height = 130, $quality = 8)
            {
                // Initiate class
                $ImageMaker =   new ImageFactory();

                // Here is just a test landscape sized image
                $source_image   =   'http://media1.santabanta.com/full6/Outdoors/Landscapes/landscapes-246a.jpg';

                // This will save the file to disk. $destination is where the file will save and with what name
            //  $destination    =   'image60px.jpg';
            //  $ImageMaker->Thumbnailer($source_image,$final_width,$final_height,$destination,$quality);

                // This example will just display to browser, not save to disk
                 $ImageMaker->Thumbnailer($source_image,$final_width,$final_height,false,$quality);
            }
    }

class ImageFactory
    {
        public      $destination;

        protected   $original;

        public  function FetchOriginal($file)
            {
                $size                       =   getimagesize($file);
                $this->original['width']    =   $size[0];
                $this->original['height']   =   $size[1];
                $this->original['type']     =   $size['mime'];
                return $this;
            }

        public  function Thumbnailer($thumb_target = '', $width = 60,$height = 60,$SetFileName = false, $quality = 80)
            {
                // Set original file settings
                $this->FetchOriginal($thumb_target);
                // Determine kind to extract from
                if($this->original['type'] == 'image/gif')
                    $thumb_img  =   imagecreatefromgif($thumb_target);
                elseif($this->original['type'] == 'image/png') {
                        $thumb_img  =   imagecreatefrompng($thumb_target);
                        $quality    =   7;
                    }
                elseif($this->original['type'] == 'image/jpeg')
                        $thumb_img  =   imagecreatefromjpeg($thumb_target);
                else
                    return false;
                // Assign variables for calculations
                $w  =   $this->original['width'];
                $h  =   $this->original['height'];
                // Calculate proportional height/width
                if($w > $h) {
                        $new_height =   $height;
                        $new_width  =   floor($w * ($new_height / $h));
                        $crop_x     =   ceil(($w - $h) / 2);
                        $crop_y     =   0;
                    }
                else {
                        $new_width  =   $width;
                        $new_height =   floor( $h * ( $new_width / $w ));
                        $crop_x     =   0;
                        $crop_y     =   ceil(($h - $w) / 2);
                    }
                // New image
                $tmp_img = imagecreatetruecolor($width,$height);
                // Copy/crop action
                imagecopyresampled($tmp_img, $thumb_img, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $w, $h);
                // If false, send browser header for output to browser window
                if($SetFileName == false)
                    header('Content-Type: '.$this->original['type']);
                // Output proper image type
                if($this->original['type'] == 'image/gif')
                    imagegif($tmp_img);
                elseif($this->original['type'] == 'image/png')
                    ($SetFileName !== false)? imagepng($tmp_img, $SetFileName, $quality) : imagepng($tmp_img);
                elseif($this->original['type'] == 'image/jpeg')
                    ($SetFileName !== false)? imagejpeg($tmp_img, $SetFileName, $quality) : imagejpeg($tmp_img);
                // Destroy set images
                if(isset($thumb_img))
                    imagedestroy($thumb_img); 
                // Destroy image
                if(isset($tmp_img))
                    imagedestroy($tmp_img);
            }
    }

    AvatarModel::resizeAvatarImage();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将一种类型的CompletableFuture转换为另一种类型?

java:如何将变量从一种类型动态转换为另一种类型?

打字稿:是否有一种简单的方法可以将一种类型的对象数组转换为另一种类型

如何将嵌套对象从一种类型转换为另一种类型

向量从一种类型隐式转换为另一种c ++

C#-将异步任务从一种类型转换为另一种类型

从一种承诺类型转换为另一种类型

在Typescript中从一种类型转换为另一种类型的最佳实践方法

将一种类型的std :: vector转换为另一种类型

将异常从一种类型转换为另一种类型

将一种类型的指针转换为另一种类型的指针的局限性

使用RxJs映射将一种类型的数组转换为另一种类型的数组

将一种类型的列表转换为另一种类型

无法从一种类型转换为其他类型

将每月数据从一种类型转换为另一种类型

在C ++中将数据从一种类型转换为另一种类型?

将一种类型的列表转换为另一种类型

PHP将所有整数转换为1种类型的值

php数组唯一比较2种类型

如何将一种类型的对象(接口)转换为另一种类型(接口)?

从一种类型转换为另一种类型时的模板类型推导

无法将一种类型转换为另一种类型错误

当两者共享界面时,为什么不能从一种类型转换为另一种类型

在c#中将一种类型转换为另一种类型

如何将一种类型的对象数组转换为另一种类型?

将一种类型的 Observable 转换为另一种类型

将消费者泛型类型转换为另一种类型

Spring Boot 应用程序中的错误无法从一种类型转换为另一种类型

有没有一种方法可以将源数组的每个元素从一种类型转换为另一种类型?