IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Notice: Undefined index: dirname


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Femme Profil pro
    Webmaster
    Inscrit en
    Novembre 2014
    Messages
    60
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 43
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Novembre 2014
    Messages : 60
    Par défaut Notice: Undefined index: dirname
    Bonjour,
    J'ai suivi un tutoriel pour créer un portfolio, j'en suis à la fin mais j'obtiens cette erreur sur ma page index :
    Notice: Undefined index: dirname n C:\wamp\www\portfolio\lib\image.php on line 23
    Pouvez-vous m'aider ?

    Voici mes pages :

    index.php
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
     
    <?php 
    //pour page accessible à tout le monde, page index sans mot de passe
    $auth = 0; 
    //http://localhost/portfolio/index.php
    include 'lib/includes.php';
    include 'lib/image.php';
    include 'partials/header.php';
     
    $select = $db->query("
    	SELECT works.name, works.id, works.slug, images.name as image_name 
    	FROM works 
    	LEFT JOIN images ON images.id = works.image_id
    ");
    $works = $select->fetchAll();
    //var_dump($works);
    ?>
     
     
    <h1> Bienvenue sur mon portfolio </h1>
     
    <div class="row">
    	<?php foreach ($works as $k => $work): ?>
    		<div class="col-sm-3">
    			<a href="view.php?id=<?= $work['id']; ?>">
    				<img src="<?= WEBROOT; ?>img/works/<?= resizedName($work['image_name'], 150, 150); ?>" alt="" />
    				<h2><?= $work['name']; ?></h2>
     
    			</a>
    		</div>
    	<?php endforeach ?>
    </div>
     
     
    <?php include 'lib/debug.php'; ?>
    <?php include 'partials/footer.php'; ?>


    page image.php
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
     
    function resizedName($file, $width, $height){
    //  _100x100.png 
        $info = pathinfo($file);
        $return = '';
        if($info['dirname'] != '.'){
            $return .= $info['dirname'] . '/';
        }
        $return .= $info['filename'] . "_$width". "x$height." . $info['extension'];
        return $return;
    }
     
     
    function resizeImage($file, $width, $height){
        # We find the right file
        $pathinfo   = pathinfo(trim($file, '/'));
        $output     = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '_' . $width . 'x' . $height . '.' . $pathinfo['extension'];
     
        # Setting defaults and meta
        $info                         = getimagesize($file);
        list($width_old, $height_old) = $info;
     
        # Create image ressource
        switch ( $info[2] ) {
            case IMAGETYPE_GIF:   $image = imagecreatefromgif($file);   break;
            case IMAGETYPE_JPEG:  $image = imagecreatefromjpeg($file);  break;
            case IMAGETYPE_PNG:   $image = imagecreatefrompng($file);   break;
            default: return false;
        }
     
        # We find the right ratio to resize the image before cropping 
        $heightRatio = $height_old / $height;
        $widthRatio  = $width_old /  $width;
     
        $optimalRatio = $widthRatio;
        if ($heightRatio < $widthRatio) {
            $optimalRatio = $heightRatio;
        }
        $height_crop = ($height_old / $optimalRatio);
        $width_crop  = ($width_old  / $optimalRatio);
     
        # The two image ressources needed (image resized with the good aspect ratio, and the one with the exact good dimensions)
        $image_crop = imagecreatetruecolor( $width_crop, $height_crop );
        $image_resized = imagecreatetruecolor($width, $height);
     
        # This is the resizing/resampling/transparency-preserving magic
        if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
            $transparency = imagecolortransparent($image);
            if ($transparency >= 0) {
                $transparent_color  = imagecolorsforindex($image, $trnprt_indx);
                $transparency       = imagecolorallocate($image_crop, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($image_crop, 0, 0, $transparency);
                imagecolortransparent($image_crop, $transparency);
                imagefill($image_resized, 0, 0, $transparency);
                imagecolortransparent($image_resized, $transparency);
            }elseif ($info[2] == IMAGETYPE_PNG) {
                imagealphablending($image_crop, false);
                imagealphablending($image_resized, false);
                $color = imagecolorallocatealpha($image_crop, 0, 0, 0, 127);
                imagefill($image_crop, 0, 0, $color);
                imagesavealpha($image_crop, true);
                imagefill($image_resized, 0, 0, $color);
                imagesavealpha($image_resized, true);
            }
        }
     
        imagecopyresampled($image_crop, $image, 0, 0, 0, 0, $width_crop, $height_crop, $width_old, $height_old);
        imagecopyresampled($image_resized, $image_crop, 0, 0, ($width_crop - $width) / 2, ($height_crop - $height) / 2, $width, $height, $width, $height);
     
     
        # Writing image according to type to the output destination and image quality
        switch ( $info[2] ) {
          case IMAGETYPE_GIF:   imagegif($image_resized, $output, 80);    break;
          case IMAGETYPE_JPEG:  imagejpeg($image_resized, $output, 80);   break;
          case IMAGETYPE_PNG:   imagepng($image_resized, $output, 9);    break;
          default: return false;
        }
     
        return true;
    }


    et page view.php
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     
    <?php
    $auth = 0;
    include 'lib/includes.php';
    include 'lib/image.php';
     
    if(!isset($_GET['slug'])){
        header("HTTP/1.1 301 Moved Permanently");
        header('Location:index.php');
        die();
    }
    $slug = $db->quote($_GET['slug']);
    $select = $db->query("SELECT * FROM works WHERE slug = $slug");
    if($select->rowCount() == 0){
        header("HTTP/1.1 301 Moved Permanently");
        header('Location:index.php');
        die();
    }
    $work = $select->fetch();
    $work_id = $work['id'];
     
    $select = $db->query("SELECT * FROM images WHERE work_id = $work_id");
    $images = $select->fetchAll();
    $title = $work['name'];
     
    include 'partials/header.php';
    ?>
     
    <h1><?= $work['name']; ?></h1>
     
    <?= $work['content']; ?>
     
    <?php foreach ($images as $k => $image): ?>
        <p>
            <img src="<?= WEBROOT; ?>/img/works/<?= $image['name']; ?>" width="100%">
        </p>
    <?php endforeach ?>
     
    <?php include 'lib/debug.php'; ?>
    <?php include 'partials/footer.php'; ?>


    Merci

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Par défaut
    Je suppose que la ligne 23 correspond à la ligne 17 du code que tu nous montres.
    Ajoute un pour savoir ce qui se passe.
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  3. #3
    Invité
    Invité(e)
    Par défaut
    Bonjour,

    Ça veut dire que l'index "dirname" n'est pas défini dans le fichier image.php à la ligne 23.

    C'est là qu'il faut regarder.

  4. #4
    Membre confirmé
    Femme Profil pro
    Webmaster
    Inscrit en
    Novembre 2014
    Messages
    60
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 43
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Novembre 2014
    Messages : 60
    Par défaut
    Merci pour vos réponses. Quand je fais un vardump de pathinfo, la variable est vide.
    Comment est-ce que je peux mettre mon lien de fichier image en direct au lieu de passer par dirname que je ne maîtrise pas du tout pour le coup ?
    Merci

Discussions similaires

  1. Réponses: 8
    Dernier message: 03/04/2007, 10h01
  2. Notice: Undefined index method post
    Par vanou dans le forum Langage
    Réponses: 8
    Dernier message: 07/03/2007, 09h33
  3. Pb erreur Notice: Undefined index: pseudo
    Par PAINCO dans le forum Langage
    Réponses: 15
    Dernier message: 19/02/2007, 18h20
  4. Notice: Undefined index
    Par abusatif dans le forum Langage
    Réponses: 4
    Dernier message: 08/02/2007, 09h03
  5. Réponses: 9
    Dernier message: 01/05/2006, 00h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo