dongqiu8375 2019-05-10 16:59
浏览 71

尝试在上传时优化图像

I have a form with a couple of text inputs and a file upload. The form works great, it uploads the file where it's supposed to and adds the reference to that file in the database.

Problem is the images are huge. So I would like the form to continue uploading the image at full size, but I want it to also upload an optimized version in a separate folder that I can display on the front end as the thumbnail.

I'm trying to implement the code from Image Optimization in PHP (May 2017 by Prosper Otemuyiwa; for Cloudinary Blog).

The code I'm using requires GD to be installed, which according to my host, it is.

This is in my functions.php file:

 function compress_image($source_url, $destination_url, $quality) { // optimizes images

       $info = getimagesize($source_url);

        if ($info['mime'] == 'image/jpeg')
              $image = imagecreatefromjpeg($source_url);

        elseif ($info['mime'] == 'image/gif')
              $image = imagecreatefromgif($source_url);

      elseif ($info['mime'] == 'image/png')
              $image = imagecreatefrompng($source_url);

        imagejpeg($image, $destination_url, $quality);
    return $destination_url;
    }

Here's my form/php:

<?php

if(isset($_POST['create_post'])) {

    $portfolio_title = $_POST['portfolio_title'];
    $portfolio_tagline = $_POST['portfolio_tagline'];
    $portfolio_image = $_FILES['image']['name'];
    $portfolio_image_temp = $_FILES['image']['tmp_name'];

  move_uploaded_file($portfolio_image_temp, "../images/$portfolio_image");

    $src_url = '../images/$portfolio_image';
    $destination_url = '../images/thumbnails/$portfolio_image'; 

    compress_image($src_url, $destination_url, 60);

  $query = "INSERT INTO portfolio(portfolio_title, portfolio_tagline, portfolio_image) ";

  $query .= "VALUES('{$portfolio_title}','{$portfolio_tagline}','{$portfolio_image}' ) ";

  $create_post_query = mysqli_query($connection, $query);

  confirm($create_post_query);
};

?>

    <form action="" method="post" enctype="multipart/form-data">
        <!-- enctype is because we're uploading an image -->

        <div class="form-group">
            <label for="portfolio_title">Title</label>
            <input type="text" class="form-control" name="portfolio_title">
        </div>

        <div class="form-group">
            <label for="portfolio_tagline">Tagline</label>
            <input type="text" class="form-control" name="portfolio_tagline">
        </div>

        <div class="form-group">
            <label for="portfolio_image">Image</label>
            <input type="file" class="form-control" name="image">
        </div>

        <div class="form-group">
            <input type="submit" class="btn btn-primary" name="create_post" value="Publish">
        </div>

    </form>

I'm hoping the compress_image function would pull the newly uploaded image as the $src_image and save the optimized file at the $destination_url, but I get the following errors:

Warning: getimagesize(../images/$portfolio_image): failed to open stream: No such file or directory in /home/playground/public_html/bootstrap-template/admin/functions.php on line 15

Warning: imagejpeg() expects parameter 1 to be resource, null given in /home/playground/public_html/bootstrap-template/admin/functions.php on line 26

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器