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);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,223评论 4 61
  • 六臂女子、冰霜男子脸色都不好看,都有些恼怒看了眼黑袍男子。 “我们驻地极为重要,整个七星海圣界一共也就九十座驻地,...
    im喵小姐阅读 189评论 0 0
  • 3D Touch简介 2015年,苹果发布了iOS9以及iphone6s/iphone6s Plus,其中最具有创...
    不冷的南风阅读 6,971评论 12 158
  • Week3 Day4 Alibaba revenues rise 54% in ‘blowout quarter’...
    huuila阅读 319评论 0 0