Rewind image and return to function

Asked

Viewed 308 times

4

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

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

but I need to pass an existing image on the server for this function, how to do?

  • Theoretically, the image exists on the server, but is temporary until PHP finishes running. You want to save it somewhere else?

2 answers

3


The code you currently have is making use of 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, just provide the path and its name:

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

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

Elaboration

The code in the question comes from a class written in PHP that clears the score of a certain image for the purpose of preventing the upload of images "to adults" or containing "nudism".

To class comes from the site phpclasses.org, site courtesy of our esteemed @mlemos, having been developed and published by Bakr Alsharif:

Image Nudity Filter: Determine if an image can contain nudity

We can observe in the method used GetScore that it invokes the method _GetImageResource passing the argument without amendment:

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

    // ...
}

Already in the method _GetImageResource, It passes the argument, again without changes, to various PHP functions, all of them expecting 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().

1

You want to pass in a function the information of an image already allocated in your folder, on 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 passes the function:

$filter = new ImageFilter;
$score = $filter->GetScore($parametro);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.