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);