Bonjour à toutes et tous,
Je rencontre un souci avec un site que j'héberge.
Je m'explique: j'ai créé, dans l'administration, une gestion d'images (pour que l'administrateur puisse choisir les images qu'il veut voir présentes dans un slider).
J'ai donc un fichier PHP, qui contient mon formulaire d'envoi:
Ensuite, j'ai le fichier image.php, qui permet de renommer et de redimensionner l'image:
Code : 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 <?php include '../lib/includes.php'; /** * SAUVEGARDER un SLIDER **/ if(isset($_POST['slug'])){ checkCsrf(); $slug = $_POST['slug']; if(preg_match('/^[a-z\-0-9]+$/', $slug)){ $name = $db->quote($_POST['name']); $slug = $db->quote($_POST['slug']); $page_id = $db->quote($_POST['page_id']); if(isset($_GET['id'])){ $id = $db->quote($_GET['id']); $update = $db->query("UPDATE sliders SET name=$name, slug=$slug, page_id=$page_id WHERE id=$id"); }else{ $db->query("INSERT INTO sliders SET name=$name, page_id=$page_id, slug=$slug"); } setFlash('Le slider a bien été ajouté'); /** * ENVOI DES IMAGES **/ $slider_id = $db->quote($_GET['id']); $files = $_FILES['images']; $images = array(); include '../lib/image.php'; foreach ($files['tmp_name'] as $k => $v) { $image = array( 'name' => $files['name'][$k], 'tmp_name' => $files['tmp_name'][$k] ); $extension = pathinfo($image['name'], PATHINFO_EXTENSION); if (in_array($extension, array('jpg', 'png'))) { $db->query("INSERT INTO images SET slider_id=$slider_id"); $image_id = $db->lastInsertId(); $image_name = $image_id . '.' . $extension; move_uploaded_file($image['tmp_name'], IMAGES . '/sliders/' . $image_name); // Redimensionnement des images resizeImage(IMAGES . '/sliders/' . $image_name, 680, 390) . $image_name = $db->quote($image_name); $db->query("UPDATE images SET name=$image_name WHERE id = $image_id"); } } header('Location:slider.php'); die(); }else{ setFlash('Le slug n\'est pas valide', 'danger'); } } /** * VOIR UN SLIDER **/ if(isset($_GET['id'])){ $id = $db->quote($_GET['id']); $select = $db->query("SELECT * FROM sliders, posts WHERE sliders.id=$id"); if($select->rowCount() == 0){ setFlash("Il n'y a pas de slider avec cet ID", 'danger'); header('Location:slider.php'); die(); } $_POST = $select->fetch(); } /** * SUPPRESSION D'UNE IMAGE **/ if(isset($_GET['delete_image'])){ checkCsrf(); $id = $db->quote($_GET['delete_image']); $select = $db->query("SELECT name, slider_id FROM images WHERE id=$id"); $image = $select->fetch(); $images=glob(IMAGES . '/works/' . pathinfo($image['name'], PATHINFO_FILENAME) . '_*x*.*'); if(is_array($images)){ foreach($images as $v){ unlink($v); } } unlink(IMAGES . '/sliders/' . $image['name']); $db->query("DELETE FROM images WHERE id=$id"); setFlash("L'image a bien été supprimée"); header('Location:slider_edit.php?id=' . $image['slider_id']); die(); } /** * RECUPERER LA LISTE DES PAGES (sur quelle page se trouve le slider?) **/ $select = $db->query("SELECT id, name, type FROM posts WHERE type='page' ORDER by id ASC"); $pages = $select->fetchAll(); $pages_list = array(); foreach($pages as $page){ $pages_list[$page['id']] = $page['name']; } /** * RECUPERER LES IMAGES **/ if(isset($_GET['id'])){ $slider_id = $db->quote($_GET['id']); $select = $db->query("SELECT id, name, slider_id FROM images WHERE slider_id=$slider_id"); $images = $select->fetchAll(); }else{ $images = array(); } include '../partials/admin_header.php'; ?> <h1>Editer un slider</h1> <div class="row"> <form action="#" method="post" enctype="multipart/form-data"> <div class="col-sm-8"> <div class="form-group"> <label for="name">Nom du slider</label> <?= input('name'); ?> </div> <div class="form-group"> <label for="slug">URL de la réalisation</label> <?= input('slug'); ?> </div> <div class="form-group"> <label for="page_id">Page sur laquelle apparait le slider:</label> <?= select('page_id', $pages_list); ?> </div> <?php foreach ($images as $k => $image): ?> <p> <img src="<?= WEBROOT; ?>img/sliders/<?= $image['name']; ?>" width="100"> <a href="?delete_image=<?= $image['id']; ?>&<?= csrf(); ?>" class="btn btn-danger" onclick="return confirm('Etes-vous certain de vouloir supprimer cette image? ?');">Supprimer</a> </p> <?php endforeach ?> <div class="form-group"> <input type="file" name="images[]"> <input type="file" name="images[]" class="hidden" id="duplicate"> </div> <p> <a href="#" class="btn btn-success" id="duplicatebtn">Ajouter une image</a> <em>(Taille max. par fichier : 10MO )</em> </p> <?= csrfInput(); ?> <button type="submit" class="btn btn-default">Enregistrer</button> </div> </form> </div> <?php ob_start(); ?> <script> (function($){ $('#duplicatebtn').click(function(e){ e.preventDefault(); var $clone = $('#duplicate').clone().attr('id', '').removeClass('hidden'); $('#duplicate').before($clone); }) }) (jQuery); </script> <?php $script = ob_get_clean(); ?> <?php include '../partials/admin_footer.php'; ?>
En local, cela fonctionne bien: dans le dossier slider, mon image de base s'enregistre comme cela: nom_image.jpg
Code : 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 <?php function resizedName($file, $width, $height){ $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; } ?>
Ensuite, un deuxième fichier est enregistré: nom_image_680x390.jpg.
Mais une fois le site hébergé chez OVH, j'ai un souci: mon nom_image.jpg est bien crée dans le dossier slider, mais pas le fichier nom_image_680x390.jpg
Je n'ai pas de message d'erreur, et mon code est exactement identique en local/chez OVH.
J'aimerai comprendre pourquoi cela arrive, et surtout comment régler le problème.
Si quelqu'un par ici pouvait m'aider, ça serait bien cool car cela fait maintenant plus d'une semaine que j'ai ce souci, et je ne trouve pas d'aide.
D'avance merci pour votre aide
Partager