在PHP中将大图像动态调整为缩略图?

在PHP中将大图像动态调整为缩略图?,php,Php,当用户请求照片时,将图像动态调整为缩略图的最佳方法是什么?图片已经上传为JPG,分辨率超过3MB?我已经从他们请求的图像的数据库中知道了文件名,例如:photo100.jpg,因此在php中创建一个缩略图,然后将新创建的缩略图t_photo100.jpg作为img src=提供将是理想的您可以使用类似的库来实现此目的 它使用ImageMagick(如果可用)和PHP GD库(随PHPV4.3.0+提供)来处理图像 配置和设置后,您可以动态生成图像缩略图,如下所示: <img src="/u

当用户请求照片时,将图像动态调整为缩略图的最佳方法是什么?图片已经上传为JPG,分辨率超过3MB?我已经从他们请求的图像的数据库中知道了文件名,例如:photo100.jpg,因此在php中创建一个缩略图,然后将新创建的缩略图t_photo100.jpg作为img src=提供将是理想的

您可以使用类似的库来实现此目的

它使用ImageMagick(如果可用)和PHP GD库(随PHPV4.3.0+提供)来处理图像

配置和设置后,您可以动态生成图像缩略图,如下所示:

<img src="/uploads/phpThumb.php?src=images/logo.png&w=100" />

<img src="/uploads/phpThumb.php?src=images/foobar.png&h=50&w=50&zc=1" />

检查以获取更多选项


希望这有帮助

您可以使用类似的库来实现此目的

它使用ImageMagick(如果可用)和PHP GD库(随PHPV4.3.0+提供)来处理图像

配置和设置后,您可以动态生成图像缩略图,如下所示:

<img src="/uploads/phpThumb.php?src=images/logo.png&w=100" />

<img src="/uploads/phpThumb.php?src=images/foobar.png&h=50&w=50&zc=1" />

检查以获取更多选项


希望这有帮助

php中有一些函数可以实现这一点。请参阅本教程,了解如何调整图像大小:


然后将所有图像请求定向到一个php文件(使用.htaccess),该文件可以找到所请求的文件,将原始图像传递给resizer,然后输出新的缩略图。php中有一些函数可以实现这一点。请参阅本教程,了解如何调整图像大小:


然后将所有图像请求定向到一个php文件(使用.htaccess),该文件可以找到请求的文件,将原始图像通过大小调整器,然后输出新的缩略图。我认为,最简单的方法是使用imagemagick():


在我看来,最简单的方法是使用imagemagick():


通常,您会使用以下内容:

<img src="/images/uploads/image50.jpg" />
<?php

header("Content-Type: image/jpeg");

$imgUrl = base64_decode($_GET["img_url"]);
$c = file_get_contents($imgUrl);
$arr = getimagesizefromstring($c);

$img = imagecreatefromstring($c);

if (!is_array($arr)) {
    //remote image is not available. Use default one.
    $c = file_get_contents("include/images/nobanner.jpg");
}

if (isset($_GET["width"])){
  //Get Width and Height
  List($Width, $Height) = getimagesize($imgUrl);

   //Calc new Size
  $w = $_GET["width"];
  $h = $Height * ($w / $Width);

  //Build the image
  //Create new Base
  $NewImageBase = imagecreatetruecolor($w, $h);

  //copy image
  imagecopyresampled($NewImageBase, $img, 0, 0, 0, 0, $w, $h, $Width, $Height);
  $img = $NewImageBase;
}

imagejpeg($img);
?>

在生成html时,将其替换为类似的内容

<img src="image.php?img_url=<?=base_64_encode("/images/uploads/image50.jpg")?>&width=128" />
&width=128”/>
然后使用如下所示的image.php文件:

<img src="/images/uploads/image50.jpg" />
<?php

header("Content-Type: image/jpeg");

$imgUrl = base64_decode($_GET["img_url"]);
$c = file_get_contents($imgUrl);
$arr = getimagesizefromstring($c);

$img = imagecreatefromstring($c);

if (!is_array($arr)) {
    //remote image is not available. Use default one.
    $c = file_get_contents("include/images/nobanner.jpg");
}

if (isset($_GET["width"])){
  //Get Width and Height
  List($Width, $Height) = getimagesize($imgUrl);

   //Calc new Size
  $w = $_GET["width"];
  $h = $Height * ($w / $Width);

  //Build the image
  //Create new Base
  $NewImageBase = imagecreatetruecolor($w, $h);

  //copy image
  imagecopyresampled($NewImageBase, $img, 0, 0, 0, 0, $w, $h, $Width, $Height);
  $img = $NewImageBase;
}

imagejpeg($img);
?>

但是请记住,每次有人访问图像时,这都会导致大量计算。您可以优化image.php文件,在创建缩略图后“保存”缩略图,并在需要时简单地返回该文件


此外,这将绕过浏览器图像缓存,在每次页面刷新时都会导致更多流量。

通常情况下,您会使用以下内容:

<img src="/images/uploads/image50.jpg" />
<?php

header("Content-Type: image/jpeg");

$imgUrl = base64_decode($_GET["img_url"]);
$c = file_get_contents($imgUrl);
$arr = getimagesizefromstring($c);

$img = imagecreatefromstring($c);

if (!is_array($arr)) {
    //remote image is not available. Use default one.
    $c = file_get_contents("include/images/nobanner.jpg");
}

if (isset($_GET["width"])){
  //Get Width and Height
  List($Width, $Height) = getimagesize($imgUrl);

   //Calc new Size
  $w = $_GET["width"];
  $h = $Height * ($w / $Width);

  //Build the image
  //Create new Base
  $NewImageBase = imagecreatetruecolor($w, $h);

  //copy image
  imagecopyresampled($NewImageBase, $img, 0, 0, 0, 0, $w, $h, $Width, $Height);
  $img = $NewImageBase;
}

imagejpeg($img);
?>

在生成html时,将其替换为类似的内容

<img src="image.php?img_url=<?=base_64_encode("/images/uploads/image50.jpg")?>&width=128" />
&width=128”/>
然后使用如下所示的image.php文件:

<img src="/images/uploads/image50.jpg" />
<?php

header("Content-Type: image/jpeg");

$imgUrl = base64_decode($_GET["img_url"]);
$c = file_get_contents($imgUrl);
$arr = getimagesizefromstring($c);

$img = imagecreatefromstring($c);

if (!is_array($arr)) {
    //remote image is not available. Use default one.
    $c = file_get_contents("include/images/nobanner.jpg");
}

if (isset($_GET["width"])){
  //Get Width and Height
  List($Width, $Height) = getimagesize($imgUrl);

   //Calc new Size
  $w = $_GET["width"];
  $h = $Height * ($w / $Width);

  //Build the image
  //Create new Base
  $NewImageBase = imagecreatetruecolor($w, $h);

  //copy image
  imagecopyresampled($NewImageBase, $img, 0, 0, 0, 0, $w, $h, $Width, $Height);
  $img = $NewImageBase;
}

imagejpeg($img);
?>

但是,请记住,每次有人访问图像时,这将导致密集的计算。您可以优化image.php文件,在创建缩略图后“保存”缩略图,并在需要时简单地返回该文件


此外,这将绕过浏览器图像缓存,在每次页面刷新时都会导致更多流量。

您可以在此处查看SimpleImage类:

我使用它,它非常好而且简单。 基本上你能做到

include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resize(250,400);
$image->save('picture2.jpg');

您可以使用此预处理一次并显示新创建的图像。

您可以在此处查看SimpleImage类:

我使用它,它非常好而且简单。 基本上你能做到

include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resize(250,400);
$image->save('picture2.jpg');
您可以使用此预处理一次并显示新创建的图像。

来自Php.net:

2您可能需要的主要功能:

1:imagecopyresampled

<?php
// The file
$filename = 'test.jpg';

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

// 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);
?>
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

2:imagecopyresized

<?php
// The file
$filename = 'test.jpg';

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

// 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);
?>
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

来自Php.net:

2您可能需要的主要功能:

1:imagecopyresampled

<?php
// The file
$filename = 'test.jpg';

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

// 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);
?>
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

2:imagecopyresized

<?php
// The file
$filename = 'test.jpg';

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

// 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);
?>
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

我也面临着类似的问题。我搜索了一下,但找不到合适的解决方案。所以,我创造了一个。这将与CDN(cloudfront)一起工作,以减少php Web服务器上的负载。我已经用不同的设备尺寸在生产中运行了它,结果令人震惊。请尝试一下:


工作原理:使用http://url/width/height/imagename. 如果它存在于CDN中,它将被提供。否则它会攻击php服务器,重新调整原始图像的大小并将其存储在CDN上,然后开始为每个请求提供服务。

我也面临类似的问题。我搜索了一下,但找不到合适的解决方案。所以,我创造了一个。这将与CDN(cloudfront)一起工作,以减少php Web服务器上的负载。我已经用不同的设备尺寸在生产中运行了它,结果令人震惊。请尝试一下:


工作原理:使用http://url/width/height/imagename. 如果它存在于CDN中,它将被提供。否则它将命中php服务器,调整原始图像的大小并将其存储在CDN上,然后开始为每个请求提供服务。

我建议您将缩略图作为单独的图像存储在服务器上。您始终可以通过html属性或css设置图像的宽度和高度,但这仍然会导致加载一个巨大的图像。将图像上载到网站时,可以使用upload.php类。可以在此处找到文档:。我建议您将缩略图作为单独的图像存储在服务器上。您始终可以通过html属性或css设置图像的宽度和高度,但这仍然会导致加载一个巨大的图像。将图像上载到网站时,可以使用upload.php类。可以在此处找到文档:。这是基于与TimThumb相同的代码吗?我曾经使用TimThumb,但现在已经了解到,由于以前的安全漏洞攻击,它不受支持。这个PHPThumb会继承这些吗?@Lee:我没有太多使用PHPThumb,但我认为它不会继承TimThumb的代码。与另一个不同的是,这一个似乎在某种程度上得到了维护——这是基于与TimThumb相同的代码吗?我曾经使用TimThumb,但现在已经了解到,由于以前的安全漏洞攻击,它不受支持。这个小矮人会继承这些吗?@Lee:我会的