亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

PHP 圖片處理

瀏覽:95日期:2022-09-09 08:10:30

圖片處理函數功能:縮放、剪切、相框、水印、銳化、旋轉、翻轉、透明度、反色處理并保存歷史記錄的思路:當有圖片有改動時自動生成一張新圖片

1、轉Base64編碼

/** * 獲取圖片的Base64編碼(不支持url) * @param $img_file 傳入本地圖片地址 * @return string */function imgToBase64($img_file) { $img_base64 = ’’; if (file_exists($img_file)) { $app_img_file = $img_file; // 圖片路徑 $img_info = getimagesize($app_img_file); // 取得圖片的大小,類型等 //echo ’<pre>’ . print_r($img_info, true) . ’</pre><br>’; list($width, $height, $type, $attr) = getimagesize($app_img_file); $fp = fopen($app_img_file, 'r'); // 圖片是否可讀權限 if ($fp) { $filesize = filesize($app_img_file); $content = fread($fp, $filesize); $file_content = chunk_split(base64_encode($content)); // base64編碼 switch ($type) { //判讀圖片類型 case 1: $img_type = 'gif'; break; case 2: $img_type = 'jpg'; break; case 3: $img_type = 'png'; break; } $img_base64 = ’data:image/png;base64,’ . $file_content;//合成圖片的base64編碼 } fclose($fp); }else{ return $img_file; } return $img_base64; //返回圖片的base64}

2、圖片旋轉

/** * 圖片旋轉 * @param $src 圖片地址 * @param $direction 1順時針90 2 逆時針90 * @return string */function imgturn($src, $direction = 1){ $ext = pathinfo($src)[’extension’]; switch ($ext) { case ’gif’: $img = imagecreatefromgif($src); break; case ’jpg’: case ’jpeg’: $img = imagecreatefromjpeg($src); break; case ’png’: $img = imagecreatefrompng($src); break; default: die(’圖片格式錯誤!’); break; } $width = imagesx($img); $height = imagesy($img); $img2 = imagecreatetruecolor($height, $width); //順時針旋轉90度 if($direction == 1){ for ($x = 0; $x < $width; $x++) { for($y=0; $y<$height; $y++) { imagecopy($img2, $img, $height - 1 - $y, $x, $x, $y, 1, 1); } } }else if($direction == 2) { //逆時針旋轉90度 for ($x = 0; $x < $height; $x++) { for($y = 0; $y < $width; $y++) { imagecopy($img2, $img, $x, $y, $width - 1 - $y, $x, 1, 1); } } } switch ($ext) { case ’jpg’: case 'jpeg': imagejpeg($img2, $src, 100); break; case 'gif': imagegif($img2, $src, 100); break; case 'png': imagepng($img2, $src, 100); break; default: die(’圖片格式錯誤!’); break; } imagedestroy($img); imagedestroy($img2);}

3、圖片壓縮

/*** 圖片壓縮處理* @param string $sFile 源圖片路徑* @param int $iWidth 自定義圖片寬度* @param int $iHeight 自定義圖片高度* @return string 壓縮后的圖片路徑*/function getThumb($sFile, $iWidth, $iHeight){ //圖片公共路徑 $public_path = ’’; //判斷該圖片是否存在 if(!file_exists($public_path . $sFile)) return $sFile; list($width, $height, $type, $attr) = getimagesize($sFile); if($width < $height){ imgturn($sFile, 2); } //判斷圖片格式(圖片文件后綴) $extend = explode('.' , $sFile); $attach_fileext = strtolower($extend[count($extend) - 1]); if (!in_array($attach_fileext, array(’jpg’,’png’,’jpeg’))){ return ’’; } //壓縮圖片文件名稱 $sFileNameS = str_replace('.' . $attach_fileext, '_' . $iWidth . ’_’ . $iHeight . ’.’ . $attach_fileext, $sFile); //判斷是否已壓縮圖片,若是則返回壓縮圖片路徑 if(file_exists($public_path . $sFileNameS)){ return $sFileNameS; } //生成壓縮圖片,并存儲到原圖同路徑下 resizeImage($public_path . $sFile, $public_path . $sFileNameS, $iWidth, $iHeight); if(!file_exists($public_path . $sFileNameS)){ return $sFile; } return $sFileNameS;}

4、生成目標圖片

/** * 生成圖片 * @param string $im 源圖片路徑 * @param string $dest 目標圖片路徑 * @param int $maxwidth 生成圖片寬 * @param int $maxheight 生成圖片高 */function resizeImage($im, $dest, $maxwidth, $maxheight) { $img = getimagesize($im); switch ($img[2]) { case 1: $im = @imagecreatefromgif($im); break; case 2: $im = @imagecreatefromjpeg($im); break; case 3: $im = @imagecreatefrompng($im); break; } $pic_width = imagesx($im); $pic_height = imagesy($im); $resizewidth_tag = false; $resizeheight_tag = false; if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) { if ($maxwidth && $pic_width > $maxwidth) { $widthratio = $maxwidth / $pic_width; $resizewidth_tag = true; } if ($maxheight && $pic_height > $maxheight) { $heightratio = $maxheight / $pic_height; $resizeheight_tag = true; } if ($resizewidth_tag && $resizeheight_tag) { if ($widthratio < $heightratio){ $ratio = $widthratio; }else{ $ratio = $heightratio; } } if ($resizewidth_tag && !$resizeheight_tag){ $ratio = $widthratio; } if ($resizeheight_tag && !$resizewidth_tag){ $ratio = $heightratio; } $newwidth = $pic_width * $ratio; $newheight = $pic_height * $ratio; if (function_exists('imagecopyresampled')) { $newim = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); } else { $newim = imagecreate($newwidth, $newheight); imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); } imagejpeg($newim, $dest); imagedestroy($newim); } else { imagejpeg($im, $dest); }}

以上就是PHP對圖片的處理的詳細內容,更多關于PHP 圖片處理的資料請關注好吧啦網其它相關文章!

標簽: PHP
相關文章:
主站蜘蛛池模板: 日韩免费黄色片 | 亚洲精品久久久久久久777 | 欧美日韩不卡一区 | 日韩一级一欧美一级国产 | 综合刺激网 | 亚洲天堂视频在线免费观看 | 国产一级精品视频 | 日本精品视频在线播放 | 男女午夜性爽快免费视频不卡 | 久久亚洲私人国产精品 | 欧美黑人巨大最猛性xxxxx | 久久视频在线 | 亚洲精品国产第一区二区三区 | 亚洲国产毛片aaaaa无费看 | 免费萌白酱国产一区二区三区 | 久草中文在线 | 亚洲欧美日韩国产专区一区 | 波多野结衣免费免费视频一区 | 国产综合精品久久久久成人影 | 午夜一级毛片免费视频 | 亚洲国产成人在线 | 精品视频亚洲 | 亚洲 自拍 欧美 综合 | 亚洲一区视频在线 | 亚洲视频免费在线观看 | caoporen免费公开视频上传 | 国产欧美一区二区成人影院 | 国产欧美久久久精品影院 | 一级一级毛片看看 | 国产精品久久久久久久久久久威 | 亚洲国产精品日韩高清秒播 | 91久久福利国产成人精品 | 亚洲在线免费观看视频 | 欧美透逼 | 国产综合成人亚洲区 | 久久久久国产精品免费看 | 成人欧美一区二区三区视频xxx | 91在线 | 亚洲 | 成年午夜性爽快免费视频不卡 | 我看毛片 | 国产在线观看网址你懂得 |