当前位置:首页 > 杂七杂八 > 正文

PHP修改图片尺寸并输出(附API)

图片尺寸什么的一直很烦人,太大?太小?占带宽?七牛有一个缩略图的API,不过可惜的是那个缩略图API只能在七牛的文件使用,变大变小都不能随心所欲!那么怎么办呢?机智的小幻有想弄了一个修改尺寸的API,直接弄进去,然后输出好了的尺寸不就OK了?

当然,这个API各种伪静态未作,如果你有兴趣,可以修改下,用这个生成修改尺寸的图片,然后由七牛缓存,这一切都是可以实现的,不过对于我这样的渣渣来说,伪静态,不会啊!

php修改图片尺寸API源码:

<?php
// The file
if($_GET["url"]){
    $filename=$_GET["url"];
// Set a maximum height and width
if($_GET["w"]){
    $width=$_GET["w"];
}else{
$width="2156";
}
if($_GET["h"]){
    $width=$_GET["h"];
}else{
$height="2156";
}

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
}else{
header('Content-Type: text/html; charset=utf-8');
echo'<meta http-equiv="refresh" content="3;url=http://tu.ihuan.me">';
echo"您输入的参数有误,请重新输入———小幻美图(tu.ihuan.me)";
}
?>

是按照比例进行修改的,欢迎各位大神挑刺!

下面弄个图像吧image.php(生成的300宽图像原图1366)

好了,当然我这里也有API的

网络图片尺寸修改API

图片尺寸修改API地址:http://tu.ihuan.me/api/image.php?url=http://xxx.xxx&w=xxx&h=xxx

其中url=后参数必须填写,其他可不填写,填写w或h其中一项则按照其中的参数成比例修改尺寸

  • url=其后接需要修改图片的网址以http或https开头
  • w=后接修改后图片宽度
  • h=后接修改后图片高度

示例:http://tu.ihuan.me/api/image.php?url=http://s.cn.bing.net/az /hprichbg/rb/PetrifiedForestNP_ZH-CN10820635777_1366x768.jpg&w=500

但是,我这里不能进行缓存,如果自己想要,可以把上面的文件弄下来,然后自己修改下,存到七牛里(真是太机智了!)

如果有写好了的伪静态规则,给我份吧!我是真的不会写,简单的还行,可是这个不简单。

好了,就这些了,欢迎大家食用,流量大的还是用自己的空间来执行吧,小站受不了!

最后打个AD:欢迎大家食用本站的随机图片等API,已经有100张图了,配合一下上面的API食用更美味!

标签:
上一篇: 下一篇:

1,585 条评论

评论加载中...
  1. Awesome post.

    2018年5月29日 20:27 评论
  2. 49楼
    这家伙可能用了美佬的代理 谷歌浏览器 Windows 7
    domain name  

    Appreciate the recommendation. Will try it out.

    2018年5月29日 15:41 评论
  3. 48楼
    这家伙可能用了美佬的代理 火狐浏览器 Windows 7
    pokervn.net  

    It’s reaⅼly а nice and uѕeful piece οf info.
    I’m һappy tһat y᧐u just shared tһis useful info ѡith
    սs. Plеase keep us informed like thіs. Thank yoս for sharing.

    2018年5月29日 08:03 评论
  4. You have mentioned very interesting points!
    ps nice internet site.

    2018年5月29日 07:19 评论
  5. Hi there! This post could not be written any better!
    Reading this post reminds me of my good old room mate!
    He always kept chatting about this. I will forward this post to him.

    Pretty sure he will have a good read. Thank you for sharing!

    2018年5月29日 04:17 评论
  6. 45楼
    这家伙可能用了美佬的代理 谷歌浏览器 Linux
    Stormy  

    There is certainly a lot to learn about this topic. I love all the points
    you have made.

    2018年5月28日 23:21 评论
  7. 44楼
    United Kingdom Internet Explorer Windows 7
    Xt_Blog  

    I think this is among the most significant information for me.
    And i’m glad reading your article. But wanna remark on some general things, The website style is ideal, the articles is really great : D.
    Good job, cheers

    2018年5月28日 20:39 评论
  8. 43楼
    这家伙可能用了美佬的代理 谷歌浏览器 Windows 10
    pokerku88  

    Ahaa, its fastidious discussion on the topic of
    this piece of writing at this place at this weblog, I have read
    all that, so now me also commenting here.

    2018年5月28日 15:24 评论
  9. 42楼
    这家伙可能用了美佬的代理 谷歌浏览器 Windows 7
    lasertest  

    Way cool! Some very valid points! I appreciate you writing this write-up and the rest of the site is extremely
    good.

    2018年5月28日 06:58 评论
  10. What i do not understood is if truth be told how you’re now not actually a lot more smartly-favored than you
    might be right now. You’re so intelligent. You realize therefore considerably in terms of this matter, made me for my part consider
    it from a lot of various angles. Its like women and men don’t seem to be involved until it is one thing to do with Woman gaga!
    Your personal stuffs great. At all times deal with it up!

    2018年5月28日 05:23 评论

发表评论

不理你。不要啊!吃饭。吃惊。吃西瓜。飞吻!恭喜!Hi纠结!膜拜!OK抛媚眼。泡泡糖。抛钱。忍!生闷气!调皮。偷看。委屈。献花。疑问?抓狂!

小提示:Ctrl+Enter快速提交助您一臂之力~
加载中……