Recover image and return to function

4

I need to pass an image file to a certain function, this function normally receives the image of an upload like this:

$filter = new ImageFilter;
$score = $filter->GetScore($_FILES['photoimg']['tmp_name']);

But I need to pass an image already on the server to this function, how do I do it?

    
asked by anonymous 04.03.2015 / 19:14

2 answers

3

The code you are currently using is a temporary file that has just been sent to the server:

$filter = new ImageFilter;
$score = $filter->GetScore($_FILES['photoimg']['tmp_name']);

To use a file that already exists, simply provide the path and name of it:

$caminho = "/caminho/completo/para/a/minha/imagem/";
$imagem = "nomeDaImage.jpg";

$filter = new ImageFilter;
$score = $filter->GetScore($caminho.$imagem);

Elaboration

The code in question comes from a class written in PHP that clears the score of a particular image for purposes of preventing uploading of images "for adults" or containing "nudism".

The class comes from site phpclasses.org , site courtesy of our estimated

Image Nudity Filter : Determining if an image can contain nudity

We can see in the method used GetScore that it invokes the method _GetImageResource passing the argument without changes:

function GetScore($image)
{
    $x = 0; $y = 0;
    $img = $this->_GetImageResource($image, $x, $y);

    // ...
}

In the _GetImageResource method, it passes the argument, again unchanged, to various PHP functions, all of which are waiting to receive a path to an existing file:

function _GetImageResource($image, &$x, &$y)
{
    $info = GetImageSize($image);

    $x = $info[0];
    $y = $info[1];

    switch( $info[2] )
    {
        case IMAGETYPE_GIF:
            return @ImageCreateFromGif($image);

        case IMAGETYPE_JPEG:
            return @ImageCreateFromJpeg($image);

        case IMAGETYPE_PNG:
            return @ImageCreateFromPng($image);

        default:
            return false;
    }
}

See documentation for getimagesize() , imagecreatefromgif() , imagecreatefromjpeg() and imagecreatefrompng() .

    
04.04.2015 / 03:09
1

You want to pass in a function the information of an image already allocated in your folder, in the server, that is, after the upload.

You can check: readdir ()

And so get the list of your images:

<?php

    if ($handle = opendir('.')) {

    while (false !== ($entry[] = readdir($handle))) {  }
        if ($entry != "." && $entry != "..") {

          echo '<pre>';
          print_r($entry);
          echo '</pre>';


        }


    closedir($handle);
}

?> 

RESULT:

Array
(
    [0] => imagem1.jpg
    [1] => imagem2.jpg
    [2] => imagem3.jpg
)

obs: This code lists the files in which it is allocated. Change the value of opendir('CAMINHO/CAMINHO') , to access other folders.

Then with the value of $ entry, in array, you pass a check to find the image you want:

<?php

    if (in_array("imagem1.jpg", $entry)) { 

        $parametro = 'imagem1.jpg';

    }else{

      $parametro = 'ARQUIVO NÃO ENCONTRADO';

    }

    echo $parametro;

?> 

Finally it passes in the function:

$filter = new ImageFilter;
$score = $filter->GetScore($parametro);
    
04.03.2015 / 21:18