【问题标题】:PHP gd make thumbnail from generated imagePHP gd 从生成的图像制作缩略图
【发布时间】:2016-06-09 14:43:01
【问题描述】:

我有一个 php 脚本 cakeChart.php 正在生成简单的蛋糕。

$image = imagecreatetruecolor(100, 100);

$white    = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray     = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$navy     = imagecolorallocate($image, 0x00, 0x00, 0x80);
$red      = imagecolorallocate($image, 0xFF, 0x00, 0x00);

imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE);


header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

在文件createThumb.php 中,我想从cakeChart.php 加载生成的图像。 类似的东西(我知道这很糟糕):

$pngImage = imagecreatefrompng("pieChart.php");

我想制作这张图片的缩略图。现在这个 php 文件的唯一参考是这个

<a href="pieChart.php" target="blank">PHP pie chart</a><br>

但我想用 tumb 替换此文本,whitch 将在createThumb.php 中生成。是否可以使用cakeChart.php 制作图像,然后使用createThumb.php 将其转换为缩略图?

【问题讨论】:

    标签: php png thumbnails gd


    【解决方案1】:

    您需要另一个名为 cakeChart.php 并调整其大小的脚本,如下所示:

    <?php
    $src = imagecreatefrompng('http://example.com/cakeChart.php');
    
    $width = imagesx($src);
    $height = imagesy($src);
    // resize to 50% of original:
    $new_width = $width * .5;
    $new_height = $height * .5;
    
    $dest = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($dest, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    header('Content-type: image/png');
    imagepng($dest);
    imagedestroy($dest);
    imagedestroy($src);
    

    然后您的 HTML 将引用该文件作为图像源:

    <a href="pieChart.php" target="blank">
        <img src="http://example.com/cakeChartThumb.php" alt="PHP pie chart">
    </a>
    

    虽然这可行,但它不能有效地使用服务器资源。即使是少量的页面查看也可能导致服务器 CPU 使用率飙升并影响性能。你真的应该一次创建这两个文件并将它们保存到磁盘,在你的 HTML 中引用它们,就像你在任何其他图像文件中一样。

    【讨论】:

      猜你喜欢
      • 2014-10-24
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 2016-04-09
      • 1970-01-01
      • 2011-07-26
      相关资源
      最近更新 更多