[{"data":1,"prerenderedAt":58},["ShallowReactive",2],{"article-92":3},{"code":4,"msg":5,"data":6,"count":14},200,"查询成功",{"id":7,"title":8,"keywords":9,"description":10,"category_id":11,"content":12,"body_html":13,"thumb_up":14,"clicks":15,"sort":14,"remark":16,"status":17,"is_open":17,"is_deleted":14,"is_top":14,"is_recommend":14,"create_time":18,"update_time":19,"image_id":20,"url":13,"member_id":14,"cate_name":21,"prev":22,"next":25,"tags":28,"words":31,"read_time":32,"comments":14,"cover":33,"relevant":34},92,"PHP对图片的处理","php,图片","图片处理函数功能：缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色处理并保存历史记录的思路：当有图片有改动时自动生成一张新图片",2,"图片处理函数功能：缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色处理并保存历史记录的思路：当有图片有改动时自动生成一张新图片\n###### 1、转Base64编码\n\n```php\n\u002F**\n * 获取图片的Base64编码(不支持url)\n * @param $img_file 传入本地图片地址\n * @return string\n *\u002F\nfunction imgToBase64($img_file) {\n    $img_base64 = '';\n    if (file_exists($img_file)) {\n        $app_img_file = $img_file; \u002F\u002F 图片路径\n        $img_info = getimagesize($app_img_file); \u002F\u002F 取得图片的大小，类型等\n        \u002F\u002Fecho '\u003Cpre>' . print_r($img_info, true) . '\u003C\u002Fpre>\u003Cbr>';\n        list($width, $height, $type, $attr) = getimagesize($app_img_file);\n        $fp = fopen($app_img_file, \"r\"); \u002F\u002F 图片是否可读权限\n        if ($fp) {\n            $filesize = filesize($app_img_file);\n            $content = fread($fp, $filesize);\n            $file_content = chunk_split(base64_encode($content)); \u002F\u002F base64编码\n            switch ($type) {           \u002F\u002F判读图片类型\n                case 1: $img_type = \"gif\";\n                    break;\n                case 2: $img_type = \"jpg\";\n                    break;\n                case 3: $img_type = \"png\";\n                    break;\n            }\n            $img_base64 = 'data:image\u002Fpng;base64,' . $file_content;\u002F\u002F合成图片的base64编码\n        }\n        fclose($fp);\n    }else{\n        return $img_file;\n    }\n    return $img_base64; \u002F\u002F返回图片的base64\n}\n```\n\n###### 2、图片旋转\n\n```php\n\u002F**\n * 图片旋转\n * @param $src 图片地址\n * @param $direction 1顺时针90   2 逆时针90\n * @return string\n *\u002F\nfunction imgturn($src, $direction = 1){\n    $ext = pathinfo($src)['extension'];\n    switch ($ext) {\n        case 'gif':\n            $img = imagecreatefromgif($src);\n            break;\n        case 'jpg':\n        case 'jpeg':\n            $img = imagecreatefromjpeg($src);\n            break;\n        case 'png':\n            $img = imagecreatefrompng($src);\n            break;\n        default:\n            die('图片格式错误!');\n            break;\n    }\n    $width = imagesx($img);\n    $height = imagesy($img);\n    $img2 = imagecreatetruecolor($height, $width);\n    \u002F\u002F顺时针旋转90度\n    if($direction == 1){\n        for ($x = 0; $x \u003C $width; $x++) {\n            for($y=0; $y\u003C$height; $y++) {\n                imagecopy($img2, $img, $height - 1 - $y, $x, $x, $y, 1, 1);\n            }\n        }\n    }else if($direction == 2) {\n        \u002F\u002F逆时针旋转90度\n        for ($x = 0; $x \u003C $height; $x++) {\n            for($y = 0; $y \u003C $width; $y++) {\n                imagecopy($img2, $img, $x, $y, $width - 1 - $y, $x, 1, 1);\n            }\n        }\n    }\n    switch ($ext) {\n        case 'jpg':\n        case \"jpeg\":\n            imagejpeg($img2, $src, 100);\n            break;\n        case \"gif\":\n            imagegif($img2, $src, 100);\n            break;\n        case \"png\":\n            imagepng($img2, $src, 100);\n            break;\n        default:\n            die('图片格式错误!');\n            break;\n    }\n    imagedestroy($img);\n    imagedestroy($img2);\n}\n```\n###### 3、图片压缩\n```php\n\u002F**\n* 图片压缩处理\n* @param string $sFile 源图片路径\n* @param int $iWidth 自定义图片宽度\n* @param int $iHeight 自定义图片高度\n* @return string  压缩后的图片路径\n*\u002F\nfunction getThumb($sFile, $iWidth, $iHeight){\n    \u002F\u002F图片公共路径\n    $public_path = '';\n    \u002F\u002F判断该图片是否存在\n    if(!file_exists($public_path . $sFile)) return $sFile;\n\n    list($width, $height, $type, $attr) = getimagesize($sFile);\n    if($width \u003C $height){\n        imgturn($sFile, 2);\n    }\n\n    \u002F\u002F判断图片格式(图片文件后缀)\n    $extend = explode(\".\" , $sFile);\n    $attach_fileext = strtolower($extend[count($extend) - 1]);\n    if (!in_array($attach_fileext, array('jpg','png','jpeg'))){\n        return '';\n    }\n    \u002F\u002F压缩图片文件名称\n    $sFileNameS = str_replace(\".\" . $attach_fileext,  \"_\" . $iWidth . '_' . $iHeight . '.' . $attach_fileext, $sFile);\n    \u002F\u002F判断是否已压缩图片，若是则返回压缩图片路径\n    if(file_exists($public_path . $sFileNameS)){\n        return $sFileNameS;\n    }\n    \u002F\u002F生成压缩图片，并存储到原图同路径下\n    resizeImage($public_path . $sFile, $public_path . $sFileNameS, $iWidth, $iHeight);\n    if(!file_exists($public_path . $sFileNameS)){\n        return $sFile;\n    }\n    return $sFileNameS;\n}\n```\n生成目标图片\n``` php\n\u002F**\n * 生成图片\n * @param string $im 源图片路径\n * @param string $dest 目标图片路径\n * @param int $maxwidth 生成图片宽\n * @param int $maxheight 生成图片高\n *\u002F\nfunction resizeImage($im, $dest, $maxwidth, $maxheight) {\n    $img = getimagesize($im);\n    switch ($img[2]) {\n        case 1:\n            $im = @imagecreatefromgif($im);\n        break;\n        case 2:\n            $im = @imagecreatefromjpeg($im);\n        break;\n        case 3:\n            $im = @imagecreatefrompng($im);\n        break;\n    }\n    $pic_width = imagesx($im);\n    $pic_height = imagesy($im);\n    $resizewidth_tag = false;\n    $resizeheight_tag = false;\n    if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {\n        if ($maxwidth && $pic_width > $maxwidth) {\n            $widthratio = $maxwidth \u002F $pic_width;\n            $resizewidth_tag = true;\n        }\n        if ($maxheight && $pic_height > $maxheight) {\n            $heightratio = $maxheight \u002F $pic_height;\n            $resizeheight_tag = true;\n        }\n        if ($resizewidth_tag && $resizeheight_tag) {\n            if ($widthratio \u003C $heightratio){\n                $ratio = $widthratio;\n            }else{\n                $ratio = $heightratio;\n            }\n        }\n        if ($resizewidth_tag && !$resizeheight_tag){\n            $ratio = $widthratio;\n        }\n        if ($resizeheight_tag && !$resizewidth_tag){\n            $ratio = $heightratio;\n        }\n        $newwidth = $pic_width * $ratio;\n        $newheight = $pic_height * $ratio;\n        if (function_exists(\"imagecopyresampled\")) {\n            $newim = imagecreatetruecolor($newwidth, $newheight);\n            imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);\n        } else {\n            $newim = imagecreate($newwidth, $newheight);\n            imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);\n        }\n        imagejpeg($newim, $dest);\n        imagedestroy($newim);\n    } else {\n        imagejpeg($im, $dest);\n    }\n}\n```\n",null,0,964,"",1,"2020-07-30 15:53:38","2026-04-19 10:45:38",136,"PHP",{"id":23,"title":24},91,"用Python实现编程语言 20 年的动态排行榜",{"id":26,"title":27},93,"PHP执行Python脚本解压，压缩文件夹操作",[29],{"id":30,"name":21},25,2950,7,"https:\u002F\u002Ftp.myong.top\u002Fstorage\u002Farticle\u002F5a\u002Fb205f7e4e0e5c670fba97db6ab7ceb.jpg",[35,38,43,48,53],{"id":26,"title":27,"create_time":36,"description":37},"2020-08-06 14:59:05","PHP是有解压缩类文件的，但在实际项目中，用户方使用Python脚本压缩，很容易出现中文解压乱码，导致文件内容丢失的情况，本文主要介绍PHP执行Python脚本解压缩出现的问题以及解决方法",{"id":39,"title":40,"create_time":41,"description":42},96,"PHP中字符串跟0做比较永远是true","2021-02-04 16:16:27","最近在项目中做数据对比的功能，发现一个特殊的情况，0跟任何字符串做比较永远返回true，特意了解了一下。",{"id":44,"title":45,"create_time":46,"description":47},13,"Win系统下配置PHP链接Oracle","2019-01-17 22:23:36","该文章内容主要介绍window系统下，php连接oracle的配置。",{"id":49,"title":50,"create_time":51,"description":52},103,"面向对象三大特性五大原则 + 低耦合高内聚[转载]","2021-10-31 23:18:58","面向对象的三大特性是\"封装、\"多态\"、\"继承\"，五大原则是\"单一职责原则\"、\"开放封闭原则\"、\"里氏替换原则\"、\"依赖倒置原则\"、\"接口分离原则\"。",{"id":54,"title":55,"create_time":56,"description":57},29,"PHP设计模式之迭代器、代理模式","2019-01-17 23:03:13","本文章讲述了php设计模式中的迭代器模式和代理模式，以及简单的应用实例",1783431656965]