Skip to content
WJunction - Webmaster Forum

remote upload checks

Status
Not open for further replies.
okay

for local uploading i do checks like:

PHP:
        $name = $_FILES['imagefile']['name']; //file name
        $type = $_FILES['imagefile']['type']; // type of file
        $size = $_FILES['imagefile']['size']; //size of the mage in bytes
        $imageKB = round(($size / 1024), 2); // bytes to KB 
        $imageMB = round(($size / 1048576), 2); // bytes to MB  
        $tmp_name = $_FILES['imagefile']['tmp_name']; //temp file name and location
        
        /* // FILE SIZES  

$file_gb = round(($filesize / 1073741824), 2); // bytes to GB  
// PHP does funny thing for files larger than 2GB   */

        $ext = substr($name, strrpos($name, '.')); //grab file extension
        
        // Requirements
        $dimension = getimagesize($tmp_name);
        $width = $dimension[0]; // uploaded image width
        $height = $dimension[1]; // uploaded image height 

        // get the extension of the file in a lower case format
         $ext = strtolower($ext);

    if (in_array($type, $allowed) && ($size <= $maxfileSize) && ( $width < $maxWidth && $height < $maxHeight )) {
        $name = time().''.$ext; //we will give an unique name, for example the time in unix time format 
        move_uploaded_file($tmp_name, $uploadDir.'/'.$name); // upload the image, eventually i want to force the image on server to use my preferred file type
}

as u see this if statement is doing the checks:

PHP:
    if (in_array($type, $allowed) && ($size <= $maxfileSize) && ( $width < $maxWidth && $height < $maxHeight )) {

so can any1 tell me how/where to add for remote uploading, heres me code:

PHP:
    $urls = explode("\n", $_POST['remoteimage']);
    $imgarray = array();

    foreach ($urls as $url) {

        $imgname = basename($url);
        $localimg = $uploadDir .'/'. $imgname;
    
        $img = file_get_contents($url);
        
        file_put_contents($localimg, $img);
        
        $exten = substr($url, strrpos($url, '.') + 1); // The "strrpos" approach to get the extension.

        $ch = curl_init($url); // Get the filesize using curl
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        $data = curl_exec($ch);
        curl_close($ch);
        if ($data === false) {
            echo 'cURL failed';
            exit;
        }

        $remoteSize = 'unknown';
        $status = 'unknown';

        if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
            $status = (int)$matches[1];
        }
        
        if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
            $remoteSize = (int)$matches[1];
        }
        $imageKB = round(($remoteSize / 1024), 2); // bytes to KB 
        $imageMB = round(($remoteSize / 1048576), 2); // bytes to MB  
        
        $newname = time().'.'.$exten; // Got the file type, works ok, not ideal way but we need to check to see its valid t ype. really should use curl.
        $newImage = $uploadDir .'/'.$newname;
        rename($localimg, $newImage);  
    }
 

9 comments

Here you go for the whole code:

PHP:
$urls = explode("\n", $_POST['remoteimage']);
$imgarray = array();

foreach ($urls as $url) {

$imgname = basename($url);
$localimg = $uploadDir .'/'. $imgname;

$allowed = array("jpg", "jpeg", "png", "gif");
 
$imgsizecheck = getimagesize($url);
$widthofimg = $imgsizecheck[0];
$heightofimg = $imgsizecheck[1];

$exten = substr($imgname, strrpos($imgname, '.') + 1);

if (in_array($exten, $allowed) && ( $widthofimg < $maxWidth && $heightofimg < $maxHeight )) {  // Checks for width and extension of filetype
    
        $ch = curl_init($url); // Get the filesize using curl
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        $data = curl_exec($ch);
        curl_close($ch);
        if ($data === false) {
            echo 'cURL failed';
            exit;
        }

        $remoteSize = 'unknown';
        $status = 'unknown';

        if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
            $status = (int)$matches[1];
        }
        
        if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
            $remoteSize = (int)$matches[1];
        }
        $imageKB = round(($remoteSize / 1024), 2); // bytes to KB 
        $imageMB = round(($remoteSize / 1048576), 2); // bytes to MB  
        
        if ($imageMB <= $maxfileSize) { // Checks for Filesize of the image
        file_put_contents($localimg, $img);
        $newname = time().'.'.$exten; // Got the file type, works ok, not ideal way but we need to check to see its valid t ype. really should use curl.
        $newImage = $uploadDir .'/'.$newname;
        rename($localimg, $newImage);  
        }
}
}
 
Hi m8,

i figured it a few mins after, but when i add this line:

PHP:
        if (in_array($exten, $allowed) && ( $imageKB <= $maxfileSize ) && ( $width2 < $maxWidth && $height2 < $maxHeight )) {

above the: file_put_contents($localimg, $img);

now i need to add an ending brace } , ive tried adding it after tha line above and the bottom line, heres my code:

PHP:
    elseif (isset($_POST['remoteUpload'])) {
    
    $urls = explode("\n", $_POST['remoteimage']);
    $imgarray = array();

    foreach ($urls as $url) {

        $imgname = basename($url);
        $localimg = $uploadDir .'/'. $imgname;
        
        // Requirements
        $pixels = getimagesize($url); // Get image size in pixels for remote.
        $width2 = $pixels[0];
        $height2 = $pixels[1];

        $exten = substr($url, strrpos($url, '.') + 1); // The "strrpos" approach to get the extension.
        
        $ch = curl_init($url); // Get the filesize using curl
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        $data = curl_exec($ch);
        curl_close($ch);
        if ($data === false) {
            echo 'cURL failed';
            exit;
        }

        $remoteSize = 'unknown';
        $status = 'unknown';

        if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
            $status = (int)$matches[1];
        }
        
        if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
            $remoteSize = (int)$matches[1];
        }
        $imageKB = round(($remoteSize / 1024), 2); // bytes to KB 
        $imageMB = round(($remoteSize / 1048576), 2); // bytes to MB 

        $img = file_get_contents($url);
        
        file_put_contents($localimg, $img);

        $newname = time().'.'.$exten; 
        
        $newImage = $uploadDir .'/'.$newname;
        
        rename($localimg, $newImage);  
    }          
    
    $param = '[IMG]'.$siteURL.'/'.$uploadDir.'/'.$newname.'[/IMG]';
    
    $BBCode2 = htmlspecialchars_decode($param);

        //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        $newThumbImg = imagecreatefromstring(file_get_contents($fileURL));
        
        $ratio = $width2 / $height2; // calculate the ratio

        if ($ratio > 1 ) {
        
            $newW = $thumbPixels;
            $newH = $thumbPixels / $ratio;
    } else {
            
            $newH = $thumbPixels;
            $newW = $thumbPixels * $ratio;
    }
    
    //function for resize image.

    if ( function_exists ( 'imagecreatetruecolor' ) ) {

            $newImg = imagecreatetruecolor($newW, $newH);
    } else {

            die("Error: Please make sure you have GD library ver 2+");
    }

    //the resizing is going on here!

    imagecopyresized($newImg, $newThumbImg, 0, 0, 0, 0, $newW, $newH, $width2, $height2);
    
    $imageFileClean = preg_replace("/Resource id #6/", "", $newname);
    //finally, save the image
    
    ImageJpeg ($newImg, $thumbDir . $newname);
     
    ImageDestroy ($newImg);
    ImageDestroy ($newThumbImg);  
    
    echo "<div align='center'><h1>You've just successfully uploaded <a href='$uploadDir/$newname'>$newname</a> with the following specifications ($imageKB KB / $imageMB MB and $width2 x $height2 pixels).</h1></div><br />
        
        <div align='center'><a href='$uploadDir/$newname' rel='lightbox'><img src='$uploadDir/$newname' width='95%' alt='$newname'></a></div><br />
        
        <table width='100%' style='margin: 0 auto;'>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Direct Image Links</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Direct Image Link:</td>
        
        <td><input tabindex='1' value='$siteURL/$uploadDir/$newname' onclick='this.focus();this.select();' size='65' /></td><tr>
        
        <tr><td class='select_text' align='right'>HTML Link:</td>
        
        <td><input tabindex='2' value='".htmlspecialchars_decode("<a href=\"$siteURL/$uploadDir/$newname\"><img src=\"$siteURL/$uploadDir/$newname\" border=\"0\"></a>")."' onclick='this.focus();this.select();' size='65'/></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> BBCode (Forums)</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Code:</td> 
        
        <td><input tabindex='1' value='".$BBCode2."' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Avatar/Profile Thumbnail</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Thumbnail (90px):</td>
        
        <td><input tabindex='1' value='$siteURL/$thumbDir$newname' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td class='select_text' align='right'>Your Custom Thumbnail:</td>
        
        <td><input tabindex='1' value='$siteURL/$customThumbDir/Custom_$newname' onclick='this.focus();this.select();' size='65' /></td></tr></table>";
    }
 
fixed, stupid me, obvious fix LOL..

I was tryna use the types array tht i used for the local uploads, i needed a seperate array 4 just extensions... so heres my code:

PHP:
elseif (isset($_POST['remoteUpload'])) {
    
    $urls = explode("\n", $_POST['remoteimage']);
    $imgarray = array();

    foreach ($urls as $url) {

        $imgname = basename($url);
        $localimg = $uploadDir .'/'. $imgname;
        
        // Requirements
        $pixels = getimagesize($url); // Get image size in pixels for remote.
        $width2 = $pixels[0];
        $height2 = $pixels[1];

        $exten = substr($url, strrpos($url, '.') + 1); // The "strrpos" approach to get the extension.
        
        $ch = curl_init($url); // Get the filesize using curl
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        $data = curl_exec($ch);
        curl_close($ch);
        if ($data === false) {
            echo 'cURL failed';
            exit;
        }

        $remoteSize = 'unknown';
        $status = 'unknown';

        if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
            $status = (int)$matches[1];
        }
        
        if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
            $remoteSize = (int)$matches[1];
        }
        $imageKB = round(($remoteSize / 1024), 2); // bytes to KB 
        $imageMB = round(($remoteSize / 1048576), 2); // bytes to MB 

        $allowedext = array("jpg", "jpeg", "png", "x-png", "gif", "bmp"); // only these for remote files, why we can't use the $allowed array is beyond me, -prolly a simple thing to do LOL.
        
        if (in_array($exten, $allowedext) && ( $imageKB <= $maxfileSize ) && ( $width2 < $maxWidth && $height2 < $maxHeight )) {
        $img = file_get_contents($url);
        
        file_put_contents($localimg, $img);

        $newname = time().'.'.$exten; 
        
        $newImage = $uploadDir .'/'.$newname;
        
        rename($localimg, $newImage);  
    }          
    
    $param = '[IMG]'.$siteURL.'/'.$uploadDir.'/'.$newname.'[/IMG]';
    
    $BBCode2 = htmlspecialchars_decode($param);

        //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        $newThumbImg = imagecreatefromstring(file_get_contents($fileURL));
        
        $ratio = $width2 / $height2; // calculate the ratio

        if ($ratio > 1 ) {
        
            $newW = $thumbPixels;
            $newH = $thumbPixels / $ratio;
    } else {
            
            $newH = $thumbPixels;
            $newW = $thumbPixels * $ratio;
    }
    
    //function for resize image.

    if ( function_exists ( 'imagecreatetruecolor' ) ) {

            $newImg = imagecreatetruecolor($newW, $newH);
    } else {

            die("Error: Please make sure you have GD library ver 2+");
    }

    //the resizing is going on here!

    imagecopyresized($newImg, $newThumbImg, 0, 0, 0, 0, $newW, $newH, $width2, $height2);
    
    $imageFileClean = preg_replace("/Resource id #6/", "", $newname);
    //finally, save the image
    
    ImageJpeg ($newImg, $thumbDir . $newname);
     
    ImageDestroy ($newImg);
    ImageDestroy ($newThumbImg);  
    
    echo "<div align='center'><h1>You've just successfully uploaded <a href='$uploadDir/$newname'>$newname</a> with the following specifications ($imageKB KB / $imageMB MB and $width2 x $height2 pixels).</h1></div><br />
        
        <div align='center'><a href='$uploadDir/$newname' rel='lightbox'><img src='$uploadDir/$newname' width='95%' alt='$newname'></a></div><br />
        
        <table width='100%' style='margin: 0 auto;'>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Direct Image Links</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Direct Image Link:</td>
        
        <td><input tabindex='1' value='$siteURL/$uploadDir/$newname' onclick='this.focus();this.select();' size='65' /></td><tr>
        
        <tr><td class='select_text' align='right'>HTML Link:</td>
        
        <td><input tabindex='2' value='".htmlspecialchars_decode("<a href=\"$siteURL/$uploadDir/$newname\"><img src=\"$siteURL/$uploadDir/$newname\" border=\"0\"></a>")."' onclick='this.focus();this.select();' size='65'/></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> BBCode (Forums)</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Code:</td> 
        
        <td><input tabindex='1' value='".$BBCode2."' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Avatar/Profile Thumbnail</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Thumbnail (90px):</td>
        
        <td><input tabindex='1' value='$siteURL/$thumbDir$newname' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td class='select_text' align='right'>Your Custom Thumbnail:</td>
        
        <td><input tabindex='1' value='$siteURL/$customThumbDir/Custom_$newname' onclick='this.focus();this.select();' size='65' /></td></tr></table>";
    }
    } // < attempting a remote upload and all checks.

now i need to add an error, like i did for local uploading, which is:

PHP:
else
        echo "<p align='center' classs='select_text'>You have submitted an invalid extension/larger file size than we accept or the dimensions are too big.</p>";

so i wanna add tht, wen i add it afteer the checks i get parse error.......

any1 can help?
 
Try this:

PHP:
elseif (isset($_POST['remoteUpload'])) {
    
    $urls = explode("\n", $_POST['remoteimage']);
    $imgarray = array();

    foreach ($urls as $url) {

        $imgname = basename($url);
        $localimg = $uploadDir .'/'. $imgname;
        
        // Requirements
        $pixels = getimagesize($url); // Get image size in pixels for remote.
        $width2 = $pixels[0];
        $height2 = $pixels[1];

        $exten = substr($url, strrpos($url, '.') + 1); // The "strrpos" approach to get the extension.
        
        $ch = curl_init($url); // Get the filesize using curl
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        $data = curl_exec($ch);
        curl_close($ch);
        if ($data === false) {
            echo 'cURL failed';
            exit;
        }

        $remoteSize = 'unknown';
        $status = 'unknown';

        if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
            $status = (int)$matches[1];
        }
        
        if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
            $remoteSize = (int)$matches[1];
        }
        $imageKB = round(($remoteSize / 1024), 2); // bytes to KB 
        $imageMB = round(($remoteSize / 1048576), 2); // bytes to MB 

        $allowedext = array("jpg", "jpeg", "png", "x-png", "gif", "bmp"); // only these for remote files, why we can't use the $allowed array is beyond me, -prolly a simple thing to do LOL.
        
        if (in_array($exten, $allowedext) && ( $imageKB <= $maxfileSize ) && ( $width2 < $maxWidth && $height2 < $maxHeight )) {
        $img = file_get_contents($url);
        
        file_put_contents($localimg, $img);

        $newname = time().'.'.$exten; 
        
        $newImage = $uploadDir .'/'.$newname;
        
        rename($localimg, $newImage);  
    
    $param = '[IMG]'.$siteURL.'/'.$uploadDir.'/'.$newname.'[/IMG]';
    
    $BBCode2 = htmlspecialchars_decode($param);

        //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        $newThumbImg = imagecreatefromstring(file_get_contents($fileURL));
        
        $ratio = $width2 / $height2; // calculate the ratio

        if ($ratio > 1 ) {
        
            $newW = $thumbPixels;
            $newH = $thumbPixels / $ratio;
    } else {
            
            $newH = $thumbPixels;
            $newW = $thumbPixels * $ratio;
    }
    
    //function for resize image.

    if ( function_exists ( 'imagecreatetruecolor' ) ) {

            $newImg = imagecreatetruecolor($newW, $newH);
    } else {

            die("Error: Please make sure you have GD library ver 2+");
    }

    //the resizing is going on here!

    imagecopyresized($newImg, $newThumbImg, 0, 0, 0, 0, $newW, $newH, $width2, $height2);
    
    $imageFileClean = preg_replace("/Resource id #6/", "", $newname);
    //finally, save the image
    
    ImageJpeg ($newImg, $thumbDir . $newname);
     
    ImageDestroy ($newImg);
    ImageDestroy ($newThumbImg);  
    
    echo "<div align='center'><h1>You've just successfully uploaded <a href='$uploadDir/$newname'>$newname</a> with the following specifications ($imageKB KB / $imageMB MB and $width2 x $height2 pixels).</h1></div><br />
        
        <div align='center'><a href='$uploadDir/$newname' rel='lightbox'><img src='$uploadDir/$newname' width='95%' alt='$newname'></a></div><br />
        
        <table width='100%' style='margin: 0 auto;'>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Direct Image Links</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Direct Image Link:</td>
        
        <td><input tabindex='1' value='$siteURL/$uploadDir/$newname' onclick='this.focus();this.select();' size='65' /></td><tr>
        
        <tr><td class='select_text' align='right'>HTML Link:</td>
        
        <td><input tabindex='2' value='".htmlspecialchars_decode("<a href=\"$siteURL/$uploadDir/$newname\"><img src=\"$siteURL/$uploadDir/$newname\" border=\"0\"></a>")."' onclick='this.focus();this.select();' size='65'/></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> BBCode (Forums)</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Code:</td> 
        
        <td><input tabindex='1' value='".$BBCode2."' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Avatar/Profile Thumbnail</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Thumbnail (90px):</td>
        
        <td><input tabindex='1' value='$siteURL/$thumbDir$newname' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td class='select_text' align='right'>Your Custom Thumbnail:</td>
        
        <td><input tabindex='1' value='$siteURL/$customThumbDir/Custom_$newname' onclick='this.focus();this.select();' size='65' /></td></tr></table>";
    }
    }   
else
        echo "<p align='center' classs='select_text'>You have submitted an invalid extension/larger file size than we accept or the dimensions are too big.</p>";      
    } // < attempting a remote upload and all checks.
 
You might be including some file

Check all the files included syntax for mistakes

Edit: What line you getting error on?

Edit again: try this naw:

PHP:
elseif (isset($_POST['remoteUpload'])) {
    
    $urls = explode("\n", $_POST['remoteimage']);
    $imgarray = array();

    foreach ($urls as $url) {

        $imgname = basename($url);
        $localimg = $uploadDir .'/'. $imgname;
        
        // Requirements
        $pixels = getimagesize($url); // Get image size in pixels for remote.
        $width2 = $pixels[0];
        $height2 = $pixels[1];

        $exten = substr($url, strrpos($url, '.') + 1); // The "strrpos" approach to get the extension.
        
        $ch = curl_init($url); // Get the filesize using curl
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        $data = curl_exec($ch);
        curl_close($ch);
        if ($data === false) {
            echo 'cURL failed';
            exit;
        }

        $remoteSize = 'unknown';
        $status = 'unknown';

        if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
            $status = (int)$matches[1];
        }
        
        if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
            $remoteSize = (int)$matches[1];
        }
        $imageKB = round(($remoteSize / 1024), 2); // bytes to KB 
        $imageMB = round(($remoteSize / 1048576), 2); // bytes to MB 

        $allowedext = array("jpg", "jpeg", "png", "x-png", "gif", "bmp"); // only these for remote files, why we can't use the $allowed array is beyond me, -prolly a simple thing to do LOL.
        
        if (in_array($exten, $allowedext) && ( $imageKB <= $maxfileSize ) && ( $width2 < $maxWidth && $height2 < $maxHeight )) {
        $img = file_get_contents($url);
        
        file_put_contents($localimg, $img);

        $newname = time().'.'.$exten; 
        
        $newImage = $uploadDir .'/'.$newname;
        
        rename($localimg, $newImage);  
    
    $param = '[IMG]'.$siteURL.'/'.$uploadDir.'/'.$newname.'[/IMG]';
    
    $BBCode2 = htmlspecialchars_decode($param);

        //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        $newThumbImg = imagecreatefromstring(file_get_contents($fileURL));
        
        $ratio = $width2 / $height2; // calculate the ratio

        if ($ratio > 1 ) {
        
            $newW = $thumbPixels;
            $newH = $thumbPixels / $ratio;
    } else {
            
            $newH = $thumbPixels;
            $newW = $thumbPixels * $ratio;
    }
    
    //function for resize image.

    if ( function_exists ( 'imagecreatetruecolor' ) ) {

            $newImg = imagecreatetruecolor($newW, $newH);
    } else {

            die("Error: Please make sure you have GD library ver 2+");
    }

    //the resizing is going on here!

    imagecopyresized($newImg, $newThumbImg, 0, 0, 0, 0, $newW, $newH, $width2, $height2);
    
    $imageFileClean = preg_replace("/Resource id #6/", "", $newname);
    //finally, save the image
    
    ImageJpeg ($newImg, $thumbDir . $newname);
     
    ImageDestroy ($newImg);
    ImageDestroy ($newThumbImg);  
    
    echo "<div align='center'><h1>You've just successfully uploaded <a href='$uploadDir/$newname'>$newname</a> with the following specifications ($imageKB KB / $imageMB MB and $width2 x $height2 pixels).</h1></div><br />
        
        <div align='center'><a href='$uploadDir/$newname' rel='lightbox'><img src='$uploadDir/$newname' width='95%' alt='$newname'></a></div><br />
        
        <table width='100%' style='margin: 0 auto;'>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Direct Image Links</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Direct Image Link:</td>
        
        <td><input tabindex='1' value='$siteURL/$uploadDir/$newname' onclick='this.focus();this.select();' size='65' /></td><tr>
        
        <tr><td class='select_text' align='right'>HTML Link:</td>
        
        <td><input tabindex='2' value='".htmlspecialchars_decode("<a href=\"$siteURL/$uploadDir/$newname\"><img src=\"$siteURL/$uploadDir/$newname\" border=\"0\"></a>")."' onclick='this.focus();this.select();' size='65'/></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> BBCode (Forums)</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Code:</td> 
        
        <td><input tabindex='1' value='".$BBCode2."' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Avatar/Profile Thumbnail</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Thumbnail (90px):</td>
        
        <td><input tabindex='1' value='$siteURL/$thumbDir$newname' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td class='select_text' align='right'>Your Custom Thumbnail:</td>
        
        <td><input tabindex='1' value='$siteURL/$customThumbDir/Custom_$newname' onclick='this.focus();this.select();' size='65' /></td></tr></table>";
    }
else
     echo "<p align='center' classs='select_text'>You have submitted an invalid extension/larger file size than we accept or the dimensions are too big.</p>";      
    }   
    } // < attempting a remote upload and all checks.
 
Last edited:
PHP:
elseif (isset($_POST['remoteUpload'])) {
    
    $urls = explode("\n", $_POST['remoteimage']);
    $imgarray = array();

    foreach ($urls as $url) {

        $imgname = basename($url);
        $localimg = $uploadDir .'/'. $imgname;
        
        // Requirements
        $pixels = getimagesize($url); // Get image size in pixels for remote.
        $width2 = $pixels[0];
        $height2 = $pixels[1];

        $exten = substr($url, strrpos($url, '.') + 1); // The "strrpos" approach to get the extension.
        
        $ch = curl_init($url); // Get the filesize using curl
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        $data = curl_exec($ch);
        curl_close($ch);
        if ($data === false) {
            echo 'cURL failed';
            exit;
        }

        $remoteSize = 'unknown';
        $status = 'unknown';

        if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
            $status = (int)$matches[1];
        }
        
        if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
            $remoteSize = (int)$matches[1];
        }
        $imageKB = round(($remoteSize / 1024), 2); // bytes to KB 
        $imageMB = round(($remoteSize / 1048576), 2); // bytes to MB 

        $allowedext = array("jpg", "jpeg", "png", "x-png", "gif", "bmp"); // only these for remote files, why we can't use the $allowed array is beyond me, -prolly a simple thing to do LOL.
        
        if (in_array($exten, $allowedext) && ( $imageKB <= $maxfileSize ) && ( $width2 < $maxWidth && $height2 < $maxHeight )) {
        $img = file_get_contents($url);
        
        file_put_contents($localimg, $img);

        $newname = time().'.'.$exten; 
        
        $newImage = $uploadDir .'/'.$newname;
        
        rename($localimg, $newImage);  
    
    $param = '[IMG]'.$siteURL.'/'.$uploadDir.'/'.$newname.'[/IMG]';
    
    $BBCode2 = htmlspecialchars_decode($param);

        //Make a 90x90px thumbnail......
        $fileURL = ($uploadDir . '/' . $newname);
        //echo $fileURL;
        
        $newThumbImg = imagecreatefromstring(file_get_contents($fileURL));
        
        $ratio = $width2 / $height2; // calculate the ratio

        if ($ratio > 1 ) {
        
            $newW = $thumbPixels;
            $newH = $thumbPixels / $ratio;
    } else {
            
            $newH = $thumbPixels;
            $newW = $thumbPixels * $ratio;
    }
    
    //function for resize image.

    if ( function_exists ( 'imagecreatetruecolor' ) ) {

            $newImg = imagecreatetruecolor($newW, $newH);
    } else {

            die("Error: Please make sure you have GD library ver 2+");
    }

    //the resizing is going on here!

    imagecopyresized($newImg, $newThumbImg, 0, 0, 0, 0, $newW, $newH, $width2, $height2);
    
    $imageFileClean = preg_replace("/Resource id #6/", "", $newname);
    //finally, save the image
    
    ImageJpeg ($newImg, $thumbDir . $newname);
     
    ImageDestroy ($newImg);
    ImageDestroy ($newThumbImg);  
    
    echo "<div align='center'><h1>You've just successfully uploaded <a href='$uploadDir/$newname'>$newname</a> with the following specifications ($imageKB KB / $imageMB MB and $width2 x $height2 pixels).</h1></div><br />
        
        <div align='center'><a href='$uploadDir/$newname' rel='lightbox'><img src='$uploadDir/$newname' width='95%' alt='$newname'></a></div><br />
        
        <table width='100%' style='margin: 0 auto;'>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Direct Image Links</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Direct Image Link:</td>
        
        <td><input tabindex='1' value='$siteURL/$uploadDir/$newname' onclick='this.focus();this.select();' size='65' /></td><tr>
        
        <tr><td class='select_text' align='right'>HTML Link:</td>
        
        <td><input tabindex='2' value='".htmlspecialchars_decode("<a href=\"$siteURL/$uploadDir/$newname\"><img src=\"$siteURL/$uploadDir/$newname\" border=\"0\"></a>")."' onclick='this.focus();this.select();' size='65'/></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> BBCode (Forums)</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Code:</td> 
        
        <td><input tabindex='1' value='".$BBCode2."' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td><h1><img src='site_images/IMGHost.png'> Avatar/Profile Thumbnail</h1></td></tr>
        
        <tr><td class='select_text' align='right'>Thumbnail (90px):</td>
        
        <td><input tabindex='1' value='$siteURL/$thumbDir$newname' onclick='this.focus();this.select();' size='65' /></td></tr>
        
        <tr><td class='select_text' align='right'>Your Custom Thumbnail:</td>
        
        <td><input tabindex='1' value='$siteURL/$customThumbDir/Custom_$newname' onclick='this.focus();this.select();' size='65' /></td></tr></table>";
    }
else {
     echo "<p align='center' classs='select_text'>You have submitted an invalid extension/larger file size than we accept or the dimensions are too big.</p>";      
}    
}   
} // < attempting a remote upload and all checks.
 
Status
Not open for further replies.

About the author

C
Banned · Joined
901
Messages
17
Reactions
18
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom