jacque99 Posted October 12, 2010 Share Posted October 12, 2010 Hello everyone, In fact, I have two scripts that work well, the first: In choosing an image via a form, then resize the image and finally registers with a new name entered by the user. The second script: Takes a portion of an image. What I tried to do: Modify the second script to allow me to: Select the image using a form Take part image Save the image with a new name entered by the user. I tried several times to mix the two scripts, but I ff all the time off. I'm listening to your advice Thank you Here are my scripts //**************************************************************************************First Script //****************************************index.php <html> <head></head> <body> <!--On affiche le formulaire d'envoi d'une image--> <center> <br /><hr /> <form method="post" enctype="multipart/form-data" action="upload.php"> <p> Le nom de l'image redimensionner avec 95 ,95 <input type="text" name="video"><br><br> <input type="file" name="fichier" size="30"> <input type="submit" name="upload" value="Uploader"> </p> </form> </center> </body> </html> //****************************************upload.php <?php $nomImage=$_POST['video']; if( isset($_POST['upload']) ) // si formulaire soumis { $content_dir = 'upload/'; // dossier où sera déplacé le fichier $tmp_file = $_FILES['fichier']['tmp_name']; if( !is_uploaded_file($tmp_file) ) { exit("Le fichier est introuvable"); } // on vérifie maintenant l'extension $type_file = $_FILES['fichier']['type']; if( !strstr($type_file, 'jpg') && !strstr($type_file, 'jpeg') && !strstr($type_file, 'png') && !strstr($type_file, 'bmp') && !strstr($type_file, 'gif') ) { exit("Le fichier n'est pas une image"); } // on copie le fichier dans le dossier de destination $ext = substr($_FILES['fichier']['name'], strrpos($_FILES['fichier']['name'], '.')); $name_file = $nomImage.$ext; echo $name_file; //fonction pour changer les dimentions des fichiers function redimensionner($file, $maxwidth, $maxheight) { list($rawwidth, $rawheight, $type) = @getimagesize($file); if ($maxwidth < $rawwidth && $rawwidth >= $rawheight) { $width = $maxwidth; $height = ($width / $rawwidth) * $rawheight; } elseif ($maxheight < $rawheight && $rawheight >= $rawwidth) { $height = $maxheight; $width = ($height /$rawheight) * $rawwidth; } else { $height = $rawheight; $width = $rawwidth; } $imgbuffer = imagecreatetruecolor($width, $height); switch($type) { case 1: $image = imagecreatefromgif($file); break; case 2: $image = imagecreatefromjpeg($file); break; case 3: $image = imagecreatefrompng($file); break; case 4: $image = imagecreatefrombmp($file); break; default: exit("Tried to create thumbnail from $file: not a valid image"); } if (!$image) exit("Image creation from $file failed for an unknown reason. Probably not a valid image."); else { imagecopyresampled($imgbuffer, $image, 0, 0, 0, 0, $width, $height, $rawwidth, $rawheight); imageinterlace($imgbuffer); switch($type) { case 1: $image = imagegif($imgbuffer, $file, 80); break; case 2: $image = imagejpeg($imgbuffer, $file, 80); break; case 3: $image = imagepng($imgbuffer, $file, 7); break; } } } if( preg_match('#[\x00-\x1F\x7F-\x9F/\\\\]#', $name_file) ) { exit("Nom de fichier non valide"); } else if( !move_uploaded_file($tmp_file, $content_dir . $name_file) ) { exit("Impossible de copier le fichier dans $content_dir"); } redimensionner($content_dir.$name_file,95, 95); } ?> //***************************************************************************************Second script: Take one part of the image index.php <?php function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource. //getting the image dimensions list($width_orig, $height_orig) = getimagesize($imgSrc); $myImage = imagecreatefrompng($imgSrc); $ratio_orig = $width_orig/$height_orig; if ($thumbnail_width/$thumbnail_height > $ratio_orig) { $new_height = $thumbnail_width/$ratio_orig; $new_width = $thumbnail_width; } else { $new_width = $thumbnail_height*$ratio_orig; $new_height = $thumbnail_height; } $x_mid = $new_width/2; //horizontal middle $y_mid = $new_height/2; //vertical middle $process = imagecreatetruecolor(round($new_width), round($new_height)); imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height); imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height); imagedestroy($process); imagedestroy($myImage); return $thumb; } //Create the thumbnail $newThumb = CroppedThumbnail("monImage.png",200,200); // And display the image... header('Content-type: image/jpeg'); imagejpeg($newThumb); ?> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.