snippets / uploadImage

Language: Php - First posted by juliend2 on 2008-04-10 02:44 (7 months, 2 weeks)
Link to the snippet: http://www.friendsnippets.org/snippet/243/

upload image (et thumbnail si on fournis une largeur pour le thumbnail (3e argument))

 1 function uploadImage($fichier,$largeurImage, $largeurThumbnail = null)
2 {
3 if (isset ($fichier)){
4 $imagename = $fichier['name'];
5 $source = $fichier['tmp_name'];
6 $target = "../img/".$imagename;
7 move_uploaded_file($source, $target);
8
9 $imagepath = $imagename;
10 $save = "../img/" . $imagepath; //This is the new file you saving
11 $file = "../img/" . $imagepath; //This is the original file
12
13 list($width, $height) = getimagesize($file) ;
14
15 $modwidth = $largeurImage; // taille image grande
16
17 $diff = $width / $modwidth;
18
19 $modheight = $height / $diff;
20
21 $tn = imagecreatetruecolor($modwidth, $modheight) ;
22 $image = imagecreatefromjpeg($file) ;
23 imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
24
25 imagejpeg($tn, $save, 100) ;
26
27 $save = "../img/sml_" . $imagepath; //This is the new file you saving
28 $file = "../img/" . $imagepath; //This is the original file
29
30 list($width, $height) = getimagesize($file) ;
31
32 $modwidth = $largeurThumbnail; // taille thumbnail
33 if( $largeurThumbnail){
34 $diff = $width / $modwidth;
35
36 $modheight = $height / $diff;
37 $tn = imagecreatetruecolor($modwidth, $modheight) ;
38 $image = imagecreatefromjpeg($file) ;
39 imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
40
41 imagejpeg($tn, $save, 100) ;
42 }
43
44 //echo "Large image: <img src='img/".$imagepath."'><br>";
45 //echo "Thumbnail: <img src='img/sml_".$imagepath."'>";
46
47 return $imagename;
48 }
49 }
In order to post a comment, you should have a friendsnippet account. Please sign-in.

0 comments

Apr '08
  • upload image (et thumbnail si on fournis une largeur pour le thumbnail (3e argument))

  • From JuJu

Common Tags



snippet History

Apr '08