Commit 84e74745 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Génération d'une image "Photo introuvable" en cas de requête infructueuse.

parent 891a4bcb
Loading
Loading
Loading
Loading
+23 −9
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use UnicaenLeocarte\Service\Photo\PhotoServiceAwareInterface;
use UnicaenLeocarte\Service\Photo\PhotoServiceAwareTrait;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\JsonModel;

class IndexController extends AbstractActionController implements PhotoServiceAwareInterface
{
@@ -36,18 +37,31 @@ class IndexController extends AbstractActionController implements PhotoServiceAw
            if ($this->checkDroitUtilisationPhoto) {
                $autorise = $this->photoService->getDroitUtilisationPhoto($id);
            }
            $content = $autorise ?
                $this->photoService->getPhoto($id) :
                $this->photoService->createPhotoNonAutorisee();

            if ($autorise) {
                $content = $this->photoService->getPhoto($id);
            }
        catch (NotFoundException $e) {
            // Requête infructueuse
            else {
                try {
                    $content = $this->photoService->createPhotoNonAutorisee();
                }
                catch (ExtensionNotLoadedException $e) {
                    // Extension 'gd' non installée
                    $content = '';
                }
            }
        }
        catch (NotFoundException $e) {
            // Requête infructueuse.
            // On tente de fabriquer une image informative...
            try {
                $content = $this->photoService->createPhotoIntrouvable();
            }
            catch (ExtensionNotLoadedException $e) {
                // Extension 'gd' non installée
                $content = '';
            }
        }

        /** @var Response $response */
        $response = $this->getResponse();
+33 −4
Original line number Diff line number Diff line
@@ -62,12 +62,41 @@ class PhotoService implements SoapClientAwareInterface
        return $imgData;
    }

    /**
     * Fabrique une image informant que la photo est introuvable.
     *
     * @return string Contenu binaire de l'image générée
     */
    public function createPhotoIntrouvable()
    {
        return $this->_createImage(
            "            |" .
            "            |" .
            "    Photo   |" .
            " introuvable");
    }

    /**
     * Fabrique une image informant que l'utilisation de la photo n'est pas autorisée.
     *
     * @return string Contenu binaire de l'image générée
     */
    public function createPhotoNonAutorisee()
    {
        return $this->_createImage(
            "            |" .
            " Utilisation |" .
            " de la photo |" .
            "non autorisée"
        );
    }

    /**
     * @param string $texte Texte avec des pipes à l'emplacement des retour à la ligne,
     *                      ex: "Utilisation | de la photo | non autorisée"
     * @return string
     */
    private function _createImage($texte)
    {
        if (! extension_loaded('gd')) {
            throw new ExtensionNotLoadedException("L'extension PHP 'gd' doit être activée pour fabriquer l'image");
@@ -76,23 +105,23 @@ class PhotoService implements SoapClientAwareInterface
        $width  = $this->generationPhotoNonAutoriseeConfig['width'];
        $height = $this->generationPhotoNonAutoriseeConfig['height'];

        $tmpImageFilePath = sys_get_temp_dir() . '/' . uniqid('unicaen-leocarte-photo-non-autorisee-') . '.png';
        $tmpImageFilePath = sys_get_temp_dir() . '/' . uniqid('unicaen-leocarte-') . '.png';

        // Create the image
        $img = imagecreatetruecolor($width, $height);

        // Create some colors
        $white = imagecolorallocate($img, 255, 255, 255);
        $grey = imagecolorallocate($img, 222, 222, 222);
        $grey = imagecolorallocate($img, 245, 245, 245);
        $black = imagecolorallocate($img, 0, 0, 0);
        imagefilledrectangle($img, 0, 0, $width - 1, $height - 1, $grey);

        // Add the text
        $text = utf8_decode("Utilisation | de la photo | non autorisée");
        $text = utf8_decode($texte);
        $x = 10;
        $y = 20;
        foreach (explode('|', $text) as $part) {
            imagestring($img, 2, $x, $y, trim($part), $black);
            imagestring($img, 2, $x, $y, $part, $black);
            $y += 15;
        }