Reputation: 11
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
Upvotes: 1
Views: 108