当前位置: 首页 > 面试题库 >

从上传的图像创建缩略图

傅穆冉
2023-03-14
问题内容

我想从用户上传的图像创建缩略图,以使图像看起来不被挤压。但也想要原始图像的副本。因此,我希望原始图像将原始图像发送到我的服务器,并创建一个拇指版本并将其发送到我的服务器,以便我可以为每个上传自己的图片。

我的用户表有2个表

`user_pic` longblob NOT NULL,
`user_pic_small` longblob NOT NULL,

我对编码的图像方面并不感到热衷,但这是到目前为止。

Imageupload.php

> <form id="myForm" action="include/media.profileimage.upload.php"
> method="POST" enctype="multipart/form-data" target="ifr1">
>                   <input type = "file" name = "image_data" class = "input_text" style="width:800px;" >    
>                   <input type = "submit" name = "submit"   class = "btn_login" value = "Upload">
>         </form>

media.profileimage.upload.php

if(isset($_FILES['image_data'])){
       if(is_uploaded_file($_FILES['image_data']['tmp_name'])) {

            // prepare the image for insertion
                $imgData =addslashes (file_get_contents($_FILES['image_data']['tmp_name']));

            // get the image info..
              $size = getimagesize($_FILES['image_data']['tmp_name']);


            // our sql query
            $creator_id     =   $_SESSION['id'];
            $sql = "UPDATE users SET user_pic='".$imgData."' WHERE id=$creator_id";
            $sql2 = "INSERT INTO userphotos(photo_ownerid,photo_ispublic, photo_name, photo_caption, photo_imagedata) VALUES ($creator_id,1,'Profile Picture','Profile Picture','$imgData')";

            // insert the image
            if(!mysql_query($sql)) {
                echo "Fail. It broke.";
            }else{
            $c=mysql_query($sql2);

                echo "<script> parent.alert('Image Uploaded','',1000);</script>";
            }
        }
    }

不胜感激任何帮助或指导。谢谢


问题答案:

更新:

如果您想利用Imagick的优势(如果已安装在服务器上)。注意:我没有使用Imagick的性质,writeFile因为我在服务器上遇到了问题。文件放置内容也一样。

<?php
/**
 * 
 * Generate Thumbnail using Imagick class
 *  
 * @param string $img
 * @param string $width
 * @param string $height
 * @param int $quality
 * @return boolean on true
 * @throws Exception
 * @throws ImagickException
 */
function generateThumbnail($img, $width, $height, $quality = 90)
{
    if (is_file($img)) {
        $imagick = new Imagick(realpath($img));
        $imagick->setImageFormat('jpeg');
        $imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
        $imagick->setImageCompressionQuality($quality);
        $imagick->thumbnailImage($width, $height, false, false);
        $filename_no_ext = reset(explode('.', $img));
        if (file_put_contents($filename_no_ext . '_thumb' . '.jpg', $imagick) === false) {
            throw new Exception("Could not put contents.");
        }
        return true;
    }
    else {
        throw new Exception("No valid image provided with {$img}.");
    }
}

// example usage
try {
    generateThumbnail('test.jpg', 100, 50, 65);
}
catch (ImagickException $e) {
    echo $e->getMessage();
}
catch (Exception $e) {
    echo $e->getMessage();
}
?>

我一直在使用它,只需在存储原始图像并使用该位置创建缩略图后执行该功能即可。根据自己的喜好编辑…

function makeThumbnails($updir, $img, $id)
{
    $thumbnail_width = 134;
    $thumbnail_height = 189;
    $thumb_beforeword = "thumb";
    $arr_image_details = getimagesize("$updir" . $id . '_' . "$img"); // pass id to thumb name
    $original_width = $arr_image_details[0];
    $original_height = $arr_image_details[1];
    if ($original_width > $original_height) {
        $new_width = $thumbnail_width;
        $new_height = intval($original_height * $new_width / $original_width);
    } else {
        $new_height = $thumbnail_height;
        $new_width = intval($original_width * $new_height / $original_height);
    }
    $dest_x = intval(($thumbnail_width - $new_width) / 2);
    $dest_y = intval(($thumbnail_height - $new_height) / 2);
    if ($arr_image_details[2] == IMAGETYPE_GIF) {
        $imgt = "ImageGIF";
        $imgcreatefrom = "ImageCreateFromGIF";
    }
    if ($arr_image_details[2] == IMAGETYPE_JPEG) {
        $imgt = "ImageJPEG";
        $imgcreatefrom = "ImageCreateFromJPEG";
    }
    if ($arr_image_details[2] == IMAGETYPE_PNG) {
        $imgt = "ImagePNG";
        $imgcreatefrom = "ImageCreateFromPNG";
    }
    if ($imgt) {
        $old_image = $imgcreatefrom("$updir" . $id . '_' . "$img");
        $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
        imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
        $imgt($new_image, "$updir" . $id . '_' . "$thumb_beforeword" . "$img");
    }
}

上述功能创建的图像具有统一的缩略图尺寸。如果图像的大小与指定的缩略图大小不同(按比例),则其顶部和底部只有空格。



 类似资料:
  • 本文向大家介绍php实现从上传文件创建缩略图的方法,包括了php实现从上传文件创建缩略图的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了php实现从上传文件创建缩略图的方法。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的php程序设计有所帮助。

  • 我正在尝试为我的项目制作一个视频上传功能。但我对ffmpeg部分有一个问题。ffmpeg已安装在我的服务器上。但我不能得到任何缩略图。我尝试使用以下代码创建缩略图: 但是当使用为和 字符串(74)”https://mywebsite.com/uploads/video/ey1kXNew_video.flv: 输入/输出错误“字符串(0)” 另外,如果我像这样使用: 和给我。 请帮帮我,我哪里错了。

  • 接口说明 上传素材的缩略图 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 POST /api/server/1.0.0/uploadImg 是否需要登录 是 请求字段说明 参数 类型 请求类型 是否必须 说明 guid string form 是 素材ID posx number form 是 相机位置X posy number form

  • 本文向大家介绍Codeigniter实现多文件上传并创建多个缩略图,包括了Codeigniter实现多文件上传并创建多个缩略图的使用技巧和注意事项,需要的朋友参考一下 该程序可以实现: 1.同时上传5张图片 2.同时生成两种尺寸的缩略图 3.保存到mysql controllers:upload.php文件: views:new_pic.view文件: 此外需要注意: 1.要一次上传几个文件,修改

  • 当一张新照片添加到应用程序的照片“集合”中时,就会调用该方法。当已知是本地图像时(即:如果照片是从应用程序内部拍摄的,或者OnActivityResult的数据参数为空),则本地参数设置为true,我尝试从媒体内容提供商获取content://uri,以便获得有效的图像id传递给Thumbnails.getThumbnail。该代码对于从相机应用程序(通过startActivityForResul

  • 我想生成以下文件格式的缩略图(第一页): PDF DOC/DOCX[MS OFFICE] PPT/PPTX[MS OFFICE]