View photo registered in the bank

Asked

Viewed 149 times

-4

I want to display a photo registered in the database that was registered in a type blob attribute. However I do not want to display the photo through a link where you click and open the photo, on the page is forwarded the id where calls the function to select and show the data and the photo. The function at the bottom:

public function selecionarPetFoto($id,$pdo){
            $sql = $pdo->prepare("SELECT Foto_pet FROM pet WHERE Cod_pet=:cdp LIMIT 1");
            $sql->BindValue(':cdp',$id,PDO::PARAM_INT);
            $sql->execute();
            $sql->fetch(PDO::FETCH_OBJ);
        }

NOTE: Everything is already registered and the data is already displayed correctly, only the photo that I can not display. Remembering is not wrong, it depends on the characteristics of your application and your operational needs.

  • The best efficient way to store and recover photos is in the file system leaving only the path recorded in the database. This is because blob and fields of variable size inflate and misalign the storage structure of lines, compromising the performance of queries.

  • 1

    Use 'base64_encode()' in the blob. If the photo is a jpeg and $row is a line returned by the query: echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Foto_pet'] ).'"/>';

  • @Augustovasques And if I want to delete the photo with id selection and using delete, delete only the image path or delete the image in the server folder where it was stored?

  • 1

    Only the way. Delete the image with unlink().

  • worked well using base64_encode(), I will study law to know which is more feasible

  • Since no one has been able to give an answer, I am presenting.

Show 1 more comment

1 answer

1


Assuming Foto_pet is a JPEG and $row a line returned by query:

echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Foto_pet'] ).'"/>';

Assuming Foto_pet is a GNP and $row a line returned by query:

echo '<img src="data:image/png;base64,'.base64_encode( $row['Foto_pet'] ).'"/>';

Remembering that the best and most efficient way to store and recover photos is via operating system leaving only the path recorded in the database.

References:

Browser other questions tagged

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