php改变图像大小_使用PHP调整图像大小

php改变图像大小

It may seem counter-intuitive to use anything other than or to resize images: why on earth would you rescale an image with a server-side technique when the same could be done easily and quickly with a modification to the image’s width and height in your code?

使用以外的其他任何东西来调整图像大小似乎是违反直觉的:为什么实际上可以通过修改图像的宽度和高度来轻松快速地使用服务器端技术来调整图像的大小?在您的代码中?

The need for a server-side image resize will often occur when you are interacting with a service external to your own site: an API that requests an image in a particular size and format, for example. Another possibility is : if you allow users to upload images to your site, you’ll want to be sure they are scaled and cropped to specific dimensions. Under such circumstances, CSS is useless: the image must be the right size and format before it is used. Another good example is social media sites’ request for a thumbnail image to associate with a web page or blog post, during a “share” or “like” action.

当您与自己站点外部的服务进行交互时,经常会需要调整服务器端图像的大小:例如,请求特定大小和格式的图像的API 。 另一种可能性是 :如果允许用户将图像上传到您的网站,则需要确保它们已缩放并裁剪为特定的尺寸。 在这种情况下,CSS是无用的:图像必须使用正确的大小和格式才能使用。 另一个很好的例子是社交媒体网站在“共享”或“喜欢”操作期间要求缩略图与网页或博客文章相关联。

Facebook分享缩略图标准 (Facebook Share Thumbnail Standards)

Facebook has very specific requirements for the thumbnail image used to illustrate a share. First, the image must be specified, usually inside a meta tag:

Facebook对用于说明共享的缩略图有非常具体的要求。 首先,必须指定图片,通常在meta标签内

<meta property="og:image" content="http://mydomain.com/images/tiny.jpg">

The thumbnail image must be at least 50 pixels high and wide, and cannot exceed 130 × 110 pixels.  In addition, the aspect ratio of the image must be at least 1:3. Due to these limitations, it’s very common to define a preset square thumbnail image for Facebook: on this blog, I create a thumbnail image for each article that is 114 × 114 pixels square.

缩略图的高度和宽度必须至少为50像素,并且不能超过130×110像素。 另外,图像的高宽比必须至少为1:3。 由于这些限制,为Facebook定义一个预设的正方形缩略图非常普遍:在这个博客上,我为每篇文章创建一个114×114像素正方形的缩略图。

Google+分享缩略图图像的限制 (Google+ Share Thumbnail Image Restrictions)

Like Facebook, Google uses a meta tag to determine the thumbnail image to use, as embedded microdata:

像Facebook一样,Google使用meta标签来确定要使用的缩略图,作为嵌入式微数据:

<meta itemprop="image" content="http://mydomain.com/images/tiny.jpg">

You’d think that if Google wanted to compete with Facebook, they’d make their rules for share thumbnails the same, but no. Thumbnail images in Google+ must be at least 120 pixels high and wide in order to be shared: otherwise, Google will just grab the first suitable image it finds on the page and use that by default.

您可能会认为,如果Google想与Facebook竞争,他们会将共享缩略图的规则设为相同,但没有。 要共享缩略图,Google +中的缩略图必须至少高120像素才能共享 :否则,Google只会抓取它在页面上找到的第一张合适的图像,并默认使用该图像。

Does this mean I have to batch process all of the images in PhotoShop to 120 × 120 and keep copies in a special folder just to have them work on Google+?

这是否意味着我必须将PhotoShop中的所有图像批量处理为120×120,并将副本保存在特殊文件夹中,以使其在Google+上正常工作?

The answer, thankfully, is no. Instead, we’ll create a script that resizes the thumbnail image when it is requested by Google. I’ll create this script as a file called googlerequest.php at the root of my site.

幸运的是,答案是否定的。 相反,我们将创建一个脚本,以在Google请求时调整缩略图的大小。 我将在网站根目录下将此脚本创建为名为googlerequest.php的文件。

First, we need the name of the image that we wish to resize. There are many ways of gaining this information: defining a static image, pulling a record from a that contains the filename, etc. In this example, I’ll assume that the image filename will be passed to googlerequest.php from the page to be shared as a URL variable named thumb:

首先,我们需要调整图像的名称。 获取此信息的方法有很多:定义静态图像,从包含文件名的中提取记录等。在此示例中,我假设图像文件名将从页面传递到googlerequest.php 。作为名为thumb的URL变量共享:

<meta itemprop="image" content="//domain.com/googlerequest.php?thumb=tiny.jpg">

Now to the script. First, we’ll get our variable:

现在到脚本。 首先,我们将获得变量

<?php
	$thumb = htmlspecialchars($_GET['thumb']);
	$filename = /images/'.$thumb;
	..
?>

These two lines get the value of the variable we passed to the script (cleaning it up with htmlspecialchars to prevent any cross-site exploits) and tells PHP where the thumbnail is to be found by concatenating it with our known folder structure.

这两行获取传递给脚本的变量的值(使用htmlspecialchars进行清理以防止任何跨站点利用),并通过将其与我们已知的文件夹结构连接来告诉PHP在哪里可以找到缩略图。

Next, I’m going to convince Google that the result of this script is an image:

接下来,我将说服Google该脚本的结果是一个图像:

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

We rewrite the header of the file – much as I’ve demonstrated in how to create CSS3 with PHP – to convince Google that it is receiving a JPEG.

我们重写了文件的标头(正如我在使用PHP创建CSS3的示例中所演示的一样),以说服Google接收到JPEG。

Next, we need to create a variable that will receive our new, resized image, specifying the new width and height:

接下来,我们需要创建一个变量,该变量将接收调整后的新图像,并指定新的宽度和高度:

$image_p = imagecreatetruecolor(120, 120);

Problem: we don’t know the filetype of the original image: it could be a GIF, PNG, or JPEG. Without knowing that information, how are we meant to convert the file?

问题:我们不知道原始图像的文件类型:它可能是GIF,PNG或JPEG。 不知道该信息,我们将如何转换文件?

There are lots of ways of determining and converting a filetype in PHP, but the easiest method in this case is to transfer the existing image into a variable with imagecreatefromstring, which autodetects the format of the original image for us:

在PHP中有很多确定和转换文件类型的方法,但是在这种情况下,最简单的方法是使用imagecreatefromstring将现有图像转换为变量,从而为我们自动检测原始图像的格式:

$image = imagecreatefromstring(file_get_contents($filename));

Then, in the penultimate and most complex line, we convert and upscale $image into $image_p. We could crop the image at the same time, but we won’t – so the first four digits specified in imagecopyresampled are set to 0:

然后,在倒数第二个也是最复杂的行中,我们将$image转换并$image_p$image_p 。 我们可以同时裁剪图像,但是不会-因此将imagecopyresampled中指定的前四位数字设置为0

imagecopyresampled($image_p, $image, 0, 0, 0, 0, 120, 120, 114, 114);

Finally, output the image. The entire googlerequest.php page:

最后,输出图像。 整个googlerequest.php页面:

<?php
	$thumb = htmlspecialchars($_GET['thumb']);
	$filename = /images/'.$thumb;
	header('Content-type: image/jpeg');
	$image_p = imagecreatetruecolor(120, 120);
	$image = imagecreatefromstring(file_get_contents($filename));
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, 120, 120, 114, 114);
imagejpeg($image_p, null, 100);
?>

Using this technique, we can resize images for any purpose, before they hit the <body> or an API request. In this case, we’ve just saved ourselves hours of work by ensuring that we can use just one image thumbnail for sharing on multiple social media services.

使用此技术,我们可以在图像达到<body>或API请求之前,出于任何目的调整图像的大小。 在这种情况下,我们通过确保仅使用一个图像缩略图即可在多个社交媒体服务上共享,从而节省了工作时间。

翻译自: https://thenewcode.com/538/Resizing-Images-With-PHP

php改变图像大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值