Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中调整和显示sql中的图像_Php_Sql_Database_Image_Resize - Fatal编程技术网

在php中调整和显示sql中的图像

在php中调整和显示sql中的图像,php,sql,database,image,resize,Php,Sql,Database,Image,Resize,我想修改下面的php代码,这样它可以一次调整一个图像的大小并显示一个图像,并带有“下一个”和“上一个”按钮,以便浏览照片。我不想要任何图像库或lightbox解决方案,而是希望照片只显示在页面上。我是php新手,所以如果有人能帮我或者给我指出正确的方向,我将非常感谢所有的帮助 $sql=“从人中选择*”; $result=mysql\u query($sql)或die(“无法访问数据库:”.mysql\u error()); while($row=mysql\u fetch\u assoc($

我想修改下面的php代码,这样它可以一次调整一个图像的大小并显示一个图像,并带有“下一个”和“上一个”按钮,以便浏览照片。我不想要任何图像库或lightbox解决方案,而是希望照片只显示在页面上。我是php新手,所以如果有人能帮我或者给我指出正确的方向,我将非常感谢所有的帮助

$sql=“从人中选择*”;
$result=mysql\u query($sql)或die(“无法访问数据库:”.mysql\u error());
while($row=mysql\u fetch\u assoc($result))
{
回声“;
回声“”;
//请注意,我们正在使用数据库中的文件名构建src字符串
回声“
”; echo$row['fname']。“.$row['lname']。”
”; 回声“

”; 回声“;

您可以在浏览器中使用
width
height
属性(或者只有一个属性将保持纵横比)对其进行缩放,但是这是不好的,原因有很多,包括带宽、页面性能和图像质量

可以使用库(如或)重新调整图像的大小

带有
IMagick
的快速示例:

$hThumb = new Imagick($path_to_file); // Source file
$hThumb->thumbnailImage($x, $y); // You can use 300, 0 to do 300 width and maintain aspect ratio.
$hThumb->stripImage();  // remove meta data
$hThumb->writeImage($path_to_thumb); // write the image to disk
注意

确保具有读/写权限。您可以使用
是否可读
是否可写
来验证此权限

加载

建议使用
AJAX
加载图像,如果使用
JQuery
或类似的库,这非常容易

$('#nextBtn').click(function() {
    var index = 0; // Store this in your image ID tag perhaps
                   // e.g. $('#theImage').attr('id').replace('image', '')
                   // where ID is imageX
    $.ajax({
       url: 'getImages.php?index=' + index,
       type: "GET",
       success: function(data) {
           var result = $.parseJSON(data);
           if (result.success) {
              // Set your image src with the new path. 
              // Use result.image_data.src etc...
           }
       }
    });
});
PHP也相对简单,结构类似于:

 <?php
    $return = array('success' => false, 'image_data' => array());
    if (isset($_GET['index']) && is_numeric($_GET['index')) {
       // Fetch your image
       $return = array(
           'success' => true,
           'image_data' => array(
              'src' => $src, 
              'title' => $title,
              'index' => $index
           )
        );

    }

    echo json_encode($return);
 ?>

**另一个音符**


正如克格勃所说,你应该在上传时调整这些图片的大小,但是,它们可能不是用户提交的,因此你也可以检查输出中是否存在拇指,并根据需要生成任何拇指。当然,不要为每个视图生成它们。

你应该在上传时调整图片大小,而不是在输出时。存储原始图片和调整大小的图片,显示小ima当用户需要时,在列表中显示ges和完整大小

来自()文档的示例代码:

本例期望gd扩展包含在php中。Martin提到的Imagick扩展功能更强大,提供了更好的界面,但很少包含在web主机上


还为您谷歌搜索了以下内容:

您想只显示一张图片,当单击“下一张/上一张”显示另一张图片时?是的,数据库中的下一张图片!好的,太棒了。感谢您的快速响应!似乎上传时调整大小是我完全能够做到的。现在,我需要的帮助是让数据库一次显示一张图片,使用“下一张/前一张”v按钮。@user1402910使用
ORDER BY
LIMIT
WHERE
子句来获取所需的图像。例如,第一次加载时,您可能只想执行
ORDER BY date DESC LIMIT 1
-然后从
ajax请求中使用
LIMIT index,1
,以便根据图像y偏移行你刚刚展示了。
 <?php
    $return = array('success' => false, 'image_data' => array());
    if (isset($_GET['index']) && is_numeric($_GET['index')) {
       // Fetch your image
       $return = array(
           'success' => true,
           'image_data' => array(
              'src' => $src, 
              'title' => $title,
              'index' => $index
           )
        );

    }

    echo json_encode($return);
 ?>
// Get new dimensions
list($width, $height) = getimagesize($filename);
$widthNew = 320; // You decide
$heightNew = 240; // You decide

// Resample
$imageNew = imagecreatetruecolor($widthNew, $heightNew);
$imageOld = imagecreatefromjpeg($filename);
imagecopyresampled($imageNew, $imageOld, 0, 0, 0, 0, $widthNew, $heightNew, $width, $height);

// Output
imagejpeg($imageNew, $newFilename, 100);