【问题标题】:Imagick PHP (How to resize each image from database?)Imagick PHP(如何从数据库调整每个图像的大小?)
【发布时间】:2017-04-26 08:46:03
【问题描述】:

在将其推入数组之前,我一直在尝试调整数据库中的每个图像的大小。该数组包含图像路径(URL),我想在其中将调整大小的图像显示到网格视图上,因为原始图像太大并且加载时间太长,因此调整大小。我一直在弄清楚逻辑,但仍然无法解决。谁能帮我解决这个问题?我的数据库包含列“照片”和“位置”。

getphoto.php

<?php

require_once('dbConnect.php');

$sql = "select * from volleyupload1";
$res = mysqli_query($con, $sql);

$result = array();

while ($row = mysqli_fetch_array($res)) {
    foreach ($result as $row['photo']) {
        $imagick = new Imagick($row['photo']);
        $imagick->thumbnailImage(300, 300, true, true);
        header("Content-Type: image/jpg");
//    echo $imagick;
    }
    array_push($result, array('url' => $row['photo'], 'location' => $row['location']));
//     $test = $row['photo'];
//   foreach ($result as $row['photo']){
//     $imagick = new Imagick($row['photo']);
//    $imagick->thumbnailImage(300, 300, true, true);
//    header("Content-Type: image/jpg");
//        echo $imagick;
//
//     }
}


echo json_encode(array("result" => $result));

mysqli_close($con);

【问题讨论】:

    标签: php image-resizing imagick


    【解决方案1】:

    我不明白“照片”和“位置”之间的区别。对于浏览器,您应该只发送 URL 而不是本地文件路径。

    无论如何,您需要将缩略图图像存储到一个新文件中,然后将 url 返回到该文件而不是原始文件。

    可能是这样的:

    while ($row = mysqli_fetch_array($res)) {
        $imagick = new Imagick($row['photo']);
        $imagick->thumbnailImage(300, 300, true, true);
    
        $thumb_path = $row['photo'] . '.thumb.jpg';
        $thumb = fopen($thumb_path, 'w'); //open new filehandle to write the thumb
        $imagick->writeImageFile($thumb);
        fclose($thumb);
    
        array_push($result, array('url' => $thumb_path, 'location' => $row['location']));
    
    }
    

    $thumb_path 需要是本地文件路径。您需要向浏览器发送一个可从 Internet 访问的 URL

    【讨论】:

    • 'photo' 包含 URL,而 'location' 只是位置的名称。因为我能够显示数组中的所有 URL,所以我试图从 URL 调整图像的大小。我尝试了上面的代码,但我收到了一些错误。
    • 您至少必须调整使用的路径值,因为我不知道其中有什么。稍微调试一下并发布有用的错误,你再次卡住了
    猜你喜欢
    • 2021-07-20
    • 2023-03-16
    • 2015-11-03
    • 2015-06-16
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    相关资源
    最近更新 更多