Jump to content

PHP Code To Transfer Images to My Server


markof cheney

Recommended Posts

Hello everyone.. This is the first PHP script I've written and was hoping to get some feedback on any possible issues with it. I've pieced this together in an attempt to download remote images and store them on my server, instead of hotlinking images. The code will be used for a forum, called up by a BBCode tag. (The user will place an image URL into the BBCode, which will transfer to this PHP script). Again, this is the first time I've coded anything in PHP and was hoping to get some pointers on anything that needs changing.. thanks

 

<?php

$url = $_GET['url'];
$url_path = parse_url($url, PHP_URL_PATH);
$name = basename($url_path);
$FileExt= substr($name, -3); 
$FileTypeMIME= array("jpg" => "image/jpeg",
                         "png" => "image/png",
                         "gif" => "image/gif");

$ContentType= $FileTypeMIME[$FileExt];
if (empty($ContentType)) die("You are not allowed to access this file!");

header("Content-Type: " . $ContentType); 

$save = "../images/". strtolower($name); 

function wtf_image ($file) {
    switch($FileTypeMIME[$FileExt]){
        case "image/jpeg":
            $im = imagecreatefromjpeg($file); //jpeg file
       imagejpeg($im, $save, 0, NULL);   //save jpeg file 
        break;
        case "image/gif":
            $im = imagecreatefromgif($file); //gif file
       imagegif($im, $save, 0, NULL);   //save gif file
      break;
      case "image/png":
          $im = imagecreatefrompng($file); //png file
     imagePNG($im, $save, 0, NULL);   //save png file
      break;
    }
    return $im;
}

if (file_exists($save)) { 
readfile($save);  
} else { 
chmod($save,0755); 
$image = wtf_image($url);
//Runs wtf_image function on $url
imagedestroy($image);   
readfile($save); 
} 
?>

Link to comment
Share on other sites

Ok, tried it out and got it working after making a few modifications. In case it may be of use to anyone..

 

<?php
$url = $_GET['url'];
$url_path = parse_url($url, PHP_URL_PATH);
$name = basename($url_path);
$save = "./hotimages/". strtolower($name); 
$FileExt= substr($name, -3);
$FileTypeMIME= array("jpg" => "image/jpeg",
                         "png" => "image/png",
                         "gif" => "image/gif");

$ContentType= $FileTypeMIME[$FileExt];
if (empty($ContentType)) die("You are not allowed to access this file!");

header("Content-Type: " . $ContentType);

if (file_exists($save)) {
readfile($save); 
} else {
    switch($FileTypeMIME[$FileExt]){
        case "image/jpeg":
            $image = imagecreatefromjpeg($url); //jpeg file
       imagejpeg($image, $save);   //save jpeg file
        break;
        case "image/gif":
            $image = imagecreatefromgif($url); //gif file
       imagegif($image, $save);   //save gif file
      break;
      case "image/png":
          $image = imagecreatefrompng($url); //png file
     imagePNG($image, $save);   //save png file
      break;
    }
imagedestroy($image);   
readfile($save);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.