Mohammad Mursaleen
Mohammad Mursaleen

Reputation: 603

How to get src of upload file using php

I am trying to use a function to create thumbnails for which I need the src of the uploaded image. How can I get that?

Following is the function that I'm interested in:

function make_thumb($src, $dest, $desired_width) {
    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $dest);
}

Your help in this regard will be appreciated.

Upvotes: 0

Views: 380

Answers (2)

Orangepill
Orangepill

Reputation: 24655

$src will be your original uploaded image

$dest will be the resized image

These will both be file system paths. Assuming that both of these paths exist under the document root you can translate those to a url by just trim off the $_SERVER["DOCUMENT_ROOT"]

  $url = str_replace($_SERVER["DOCUMENT_ROOT"], realpath($dest)); 

Upvotes: 1

Rob W
Rob W

Reputation: 9142

The actual image source is in $source_image (it will be a resource).

You can always file_get_contents($src) if you wanted the raw data...

Upvotes: 0

Related Questions