Copy of image -> smaller

From: Date: Mon, 15 May 2006 07:03:52 +0000
Subject: Copy of image -> smaller
Groups: php.general 
Request: Send a blank email to php-general+get-236173@lists.php.net to get a copy of this message
Hi there! When I upload a picture from a form, then I want to create a copy with a smaller image. For example: I upload a picture with dimensions 200x150 name 4.jpg. I also want a copy of this image but with the dimensions 100x75 pixels. I've tried this below, but I'm missing something I think... :-) I'm using PHP 4.x (don't know exactly the vers.nr, but I can search for it if it is of importance) The code down below is a function for uploading a picture. The part I want help with is after the comment: //What should/could I do here? Best regards Gustav Wiberg <?php function uploadPic($idUpload, $picUpload, $addUpload, $copyFile, $toPath) { //Upload chosen file to upload-map ( // if (strlen($_FILES[$picUpload]['name'])>0) { //ECHO "yes! ID=$idUpload PIC=$picUpload ADD=$addUpload<br>"; $uploaddir = dirname($_FILES[$picUpload]['tmp_name']) . "/"; //Replace .jpeg to .jpg // $_FILES[$picUpload]['name'] = str_replace(".jpeg",".jpg",$_FILES[$picUpload]['name']); //Get first 4 last characters of uploaded filename // $ble = strtolower(substr($_FILES[$picUpload]['name'], -4, 4)); //Move to path $toPath (followed by what to add after file (that is sent to this function)) and ext.) // $mfileAdd = $idUpload . $addUpload . $ble; move_uploaded_file($_FILES[$picUpload]['tmp_name'], $toPath . $mfileAdd); //echo "mfileAdd=$mfileAdd<br><br>"; //Set appropiate rights for file // //echo "FILE TO TEST=$mfileAdd"; chmod($toPath . $mfileAdd, intval('0755', 8)); //Copy this file to another file? // if (strlen($copyFile)>0) { $mfile = $idUpload . $copyFile . $ble; //echo "MFILE=$mfile"; copy($toPath . $mfileAdd, $toPath . $mfile); chmod($toPath . $mfile, intval('0755', 8)); } //What should/could I do here? //
           //Set new width and height
           //
          $new_width = 100;
          $new_height = 200;
          $tmp_image=imagecreatefromjpeg($toPath . $mfileAdd);
          $width = imagesx($tmp_image);
          $height = imagesy($tmp_image);
          $new_image = imagecreatetruecolor($new_width,$new_height);
           ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
           //Grab new image
           ob_start();
           ImageJPEG($new_image);
           $image_buffer = ob_get_contents();
           ob_end_clean();
           ImageDestroy($new_image);
           //Create temporary file and write to it
           $fp = tmpfile();
           fwrite($fp, $image_buffer);
           rewind($fp);
           //Upload new image
           $copyTo = 'http://www.ledins.se/test.jpg';
           $conn_id = ftp_connect('<domain>');
           ftp_login($conn_id,'<username for domain>','<password>');
           ftp_fput($conn_id, $copyTo, $fp, FTP_BINARY);
           fclose($fp);
//Return the filename created based on productID // return $mfileAdd; } }

« previous php.general (#236173) next »