thinkphp processes base64 images

WBOY
Release: 2016-10-17 09:30:16
Original
3534 people have browsed it

<code>        $url = ''//网络图片地址;
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, '');
        curl_setopt($curl, CURLOPT_REFERER, '');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        // header('Content-type: image/JPEG');
        // echo $result;
        return $result;
        </code>
Copy after login
Copy after login

How to apply the examples given by tp

<code>$image = \think\Image::open('./image.png');
// 返回图片的宽度
$width = $image->width(); 
// 返回图片的高度
$height = $image->height(); 
// 返回图片的类型
$type = $image->type(); 
// 返回图片的mime类型
$mime = $image->mime(); 
// 返回图片的尺寸数组 0 图片宽度 1 图片高度

$size = $image->size(); 
</code>
Copy after login
Copy after login

How to use thinkphp to process the $result? For example, I want to change the width and height of the image and then upload it to the server.
Thank you!

Reply content:

<code>        $url = ''//网络图片地址;
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, '');
        curl_setopt($curl, CURLOPT_REFERER, '');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        // header('Content-type: image/JPEG');
        // echo $result;
        return $result;
        </code>
Copy after login
Copy after login

How to apply the examples given by tp

<code>$image = \think\Image::open('./image.png');
// 返回图片的宽度
$width = $image->width(); 
// 返回图片的高度
$height = $image->height(); 
// 返回图片的类型
$type = $image->type(); 
// 返回图片的mime类型
$mime = $image->mime(); 
// 返回图片的尺寸数组 0 图片宽度 1 图片高度

$size = $image->size(); 
</code>
Copy after login
Copy after login

How to use thinkphp to process the $result? For example, I want to change the width and height of the image and then upload it to the server.
Thank you!

This is what I installed to encapsulate the native version on the Internet. For your reference, it is actually very simple, just decode the base64 bit and save it locally.
The one below is just generated.
As for the size of the operating image you mentioned, as far as I know, you either use a js plug-in to let the user take a screenshot before uploading, or you generate it locally and then use PHP to operate it. The server can only operate local files if it wants to operate files.

<code>
/**
 * 保存64位编码图片
 */

 function saveBase64Image($base64_image_content){

        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){

                  //图片后缀
                  $type = $result[2];

                  //保存位置--图片名
                  $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
                  $image_url = '/uploads/image/'.date('Ymd').'/'.$image_name;           
                  if(!is_dir(dirname('.'.$image_url))){
                         mkdir(dirname('.'.$image_url));
                        chmod(dirname('.'.$image_url), 0777);
                        umask($oldumask);

                  }
                 
                  //解码
                  $decode=base64_decode(str_replace($result[1], '', $base64_image_content));
                  if (file_put_contents('.'.$image_url, $decode)){
                        $data['code']=0;
                        $data['imageName']=$image_name;
                        $data['url']=$image_url;
                        $data['msg']='保存成功!';
                  }else{
                    $data['code']=1;
                    $data['imgageName']='';
                    $data['url']='';
                    $data['msg']='图片保存失败!';
                  }
        }else{
            $data['code']=1;
            $data['imgageName']='';
            $data['url']='';
            $data['msg']='base64图片格式有误!';


        }       
        return $data;


 }</code>
Copy after login

Temporarily save it as image.png
Then use thinnkphp’s image class to operate it, and then delete the temporary image after it is done.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template