PHP从URL调整大小 [英] PHP resize from URL

查看:120
本文介绍了PHP从URL调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编辑了基本的php.net脚本,以调整大小以适合我的规格.它适用于本地文件,但涉及远程文件(这是尝试调整wordpress博客的图像大小以配合博客文章),我似乎无法弄清楚如何抓取图像,对其进行处理然后输出

I've edited the basic php.net script for resizing to work for my specs. It works with local files but when it comes to remote files (this is trying to resize images from a wordpress blog to go with the blog posts) I can't seem to figure out how to GRAB the image, manipulate it, then output it.

// The file
$filename = 'like-father-like-son.jpg';

// Set a maximum height and width
$width = 150;
$height = 150;

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

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

上面的脚本有效,但是我希望$ filename可以访问: http://USERNAME.files.wordpress.com/DATE/like-father-like-son.jpg .

The above script works, but I want $filename to access: http://USERNAME.files.wordpress.com/DATE/like-father-like-son.jpg .

此脚本运行正常.我试图在本地运行WAMP的网络出现问题.那会教我在上班时间的午餐时间尝试做! 很抱歉浪费任何人的时间,并感谢那些做出贡献的人.

This script works fine. It was a problem with the network I was trying to run WAMP locally on. That'll teach me to try doing it during lunchtime at work! Apologies for wasting anyone's time and THANK YOU to those who contributed.

请随时使用以下脚本,它会调整文件或URL中图像的大小,使其适合声明的尺寸(只要是jpeg),并保持相同的宽高比.

Please feel free to use the script below, it resizes an image from file or URL so that it fits within the dimensions declared (as long as its a jpeg) and keeps the same aspect ratio.

我以为我会在这里添加它,因为我在实现它时遇到了麻烦.我个人还有两个问题,我想用它来生成图像,就像打印一些带有'header('Content-Type:image/jpeg');的文本一样.不起作用.我还希望调整大小脚本能够与多个图像一起运行.

EDIT 2: I thought I'd just add this here since I had this trouble implementing it. I personally had two further problems, I wanted to use this to produce an image AS WELL as printing some text which with the 'header('Content-Type: image/jpeg'); doesn't work. I also wanted the resize script to run with more than one image.

解决方案:将图像调整大小脚本放置在单独的文件(imageresizer.php)中,然后使用GET数据向其发送图像的URL.因此,图像调整大小脚本的顶部现在应显示为:

Solution: Place the image resize script in a separate file (imageresizer.php) and use GET data to send the image's URL to it. So the top part of the image resize script would now read:

// The file
$filename = $_GET['imagesrc'];

在底部,我得到了图像返回:

and at the bottom I had the image return:

//Output
return (imagejpeg($image_p,null,100));

用于引用和渲染图像的脚本(将数据发送到图像缩放器的脚本)将类似于以下内容:

The script to reference and render the images (the one sending the data to image resizer) would have something resembling this:

$imagesrc = "put your image sourcing script here";
echo '<img src="imageresizer.php?' . $imagesrc . '" />';

我希望这对外面的人有帮助!

I hope this helps someone out there!

推荐答案

呵呵,看来imagecreatefromjpeg接受作为参数url,看看

Huh, it seems that imagecreatefromjpeg accepts as a parameter url, take a look http://www.php.net/manual/en/function.imagecreatefromjpeg.php

imagecreatefromjpeg —从文件或URL创建新图像

imagecreatefromjpeg — Create a new image from file or URL

getimagesize也可以使用URL,在php 4.0.5中添加了suport http://www.php.net/manual/zh/function.getimagesize.php

getimagesize works with URL too, suport was added in php 4.0.5 http://www.php.net/manual/en/function.getimagesize.php

4.0.5    URL support was added.

这篇关于PHP从URL调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆