Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP图像上传-从一种类型转变为多种类型_Php_Image_Upload_Gd - Fatal编程技术网

PHP图像上传-从一种类型转变为多种类型

PHP图像上传-从一种类型转变为多种类型,php,image,upload,gd,Php,Image,Upload,Gd,目前我可以上传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

目前我可以上传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();