php圖片處理舉例

1.php壓縮圖片程序

單純壓縮圖片 輸出到瀏覽器

<?php 
Header("Content-type: image/PNG");/*告訴IE瀏覽器你做的程序是張圖片*/
$image = @imagecreatefrompng ("banner.png"); 
imagepng ($image,null,0); /*壓縮等級0-9,壓縮后9最小,1最大*/
imagedestroy ($image);
?>

2.php成比例縮放

<?php

/*
File: thumbs.php
Example: ![](thumbs.php?filename=photo.jpg&width=100&height=100)
*/
  
$filename= $_GET['filename'];
$width = $_GET['width'];
$height = $_GET['height'];
$path=""; //finish in "/"
  
  
// Content type
header('Content-type: image/jpeg');
  
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($path.$filename);
  
if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;
}
  
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($path.$filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  
// Output
imagejpeg($image_p, null, 100);
  
// Imagedestroy
imagedestroy ($image_p);
?>

3.已知鏈接的圖片抓取

            $head_img = file_get_contents($img);
            $head_url = 'Public/headico/user_' . $value['id'] . '.jpg';
            file_put_contents($head_url, $head_img);

4.正則抓取所有圖片

$re = file_get_contents($value);
preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $re, $match);
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容