Greeetings
I'm using PHP 8.1.2
and Cloudinary
I'm still new to Cloudinary
with PHP
.
I have a script that uploads an image to Cloudinary
. It works just fine. On the flip-side, I don't know how to display an image that is stored on Cloudinary
. I've found very little help online. I don't know what Cloudinary
files/classes I need to include/require. I'm doing something wrong.
If anyone can point me to a good how-to tutorial or show me how to do this, I would be most greatful.
# show-image.html
<?php
require_once('../vendor/autoload.php');
use Cloudinary\Cloudinary;
// image folder/name on cloudinary
$image_on_cloudinary = 'student_images/carter-ski-jump-big.jpg';
$cld_image = new Cloudinary();
$cld_image->imageTag($image_on_cloudinary); // line 9 referenced in error
?>
I get this error:
[30-Jan-2024 21:30:45 UTC] PHP Fatal error: Uncaught Cloudinary\Exception\ConfigurationException:
Invalid configuration, please set up your environment in /home/sacnomad/livingmuaythai.com/vendor/cloudinary/cloudinary_php/src/Configuration/Configuration.php:293
Stack trace:
#0 /home/sacnomad/livingmuaythai.com/vendor/cloudinary/cloudinary_php/src/Cloudinary.php(59): Cloudinary\Configuration\Configuration->validate()
#1 /home/sacnomad/livingmuaythai.com/students/show-image.html(9): Cloudinary\Cloudinary->__construct()
#2 {main}
thrown in /home/sacnomad/livingmuaythai.com/vendor/cloudinary/cloudinary_php/src/Configuration/Configuration.php on line 293
In my image upload script, I have this set up, which I also tried in my show-image page, minus the Cloudinary upload lines
.
<?php
require_once('../includes/cloudinary_creds.php');
require_once('../vendor/autoload.php');
$cc = new CloudinaryCreds();
use Cloudinary\Cloudinary;
use Cloudinary\Configuration\Configuration;
Configuration::instance( $cc->get_cloudinary_url() );
// Use the UploadApi class for uploading assets
use Cloudinary\Api\Upload\UploadApi;
$upload = new UploadApi();
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
if( isset($action) && $action=="Submit" ){
if ( is_uploaded_file($_FILES['uploaded_file']['tmp_name']) ) {
$tmp_name = $_FILES["uploaded_file"]["tmp_name"];
$image_name = $_FILES["uploaded_file"]["name"];
$image_array = $upload->upload($tmp_name, [
'public_id' => $image_name,
'use_filename' => TRUE,
'folder' => 'student_images',
'overwrite' => TRUE ] );
print "<p>Public_ID: ".$image_array['public_id']."</p>";
}
}
...
As noted, this works just fine for uploading an image. No errors.
You'll need to pass the config when instantiating a Cloudinary object so that the image_tag call will generate the image url to an existing account. For example:
//call the image
$image_on_cloudinary = 'student_images/carter-ski-jump-big.jpg';
$config = Configuration::instance();
$config->cloud->cloudName = 'my_cloud_name';
$config->cloud->apiKey = 'my_key';
$config->cloud->apiSecret = 'my_secret';
$config->url->secure = true;
$cld_image = new Cloudinary($config);
$cld_image->imageTag($image_on_cloudinary);
The above should generate the image URL.