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 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)