mcontreira Posted August 25, 2010 Share Posted August 25, 2010 Hello everybody, I created a function called 'imgCreate ()' to resize an image. I'm trying to use this function to recreate images in two sizes that I need from a base image received by upload. The question is, the two images are created from the base image that I keep in a folder 'uploads', the original image is saved correctly now two other file is created, but it only comes with zero bytes and the image does not appear, or is not properly created. Another thing also happened is that the images began to be displayed in binary. I tried to insert the header ("Content-type: image / jpeg) but still does not work. I've tried several ways to correct but still not had success, if someone can me better target for correction of these errors will be immensely grateful. Below is the code I'm using. Function 'imgCreate()' in file 'funcoes.php': <?php function imgCreate($imgjpg, $width_target) { $img = imagecreatefromjpeg($imgjpg); $width_origin = imagesx($img); $heigth_origin = imagesy($img); $heigth_new = (int)($heigth_origin * $width_target)/$width_origin; $new = imagecreatetruecolor($width_target,$heigth_new); $criado = imagecopyresampled($new, $img, 0, 0, 0, 0, $width_target, $heigth_new, $width_origin, $heigth_origin); if($criado){ header("Content-type: image/jpeg"); echo imagejpeg($new, $arquivo, 100); } else return "Not possible create image!"; } ?> Code where I'm trying to use the function: <?php require 'funcoes.php'; $photo = $_FILES["photo"]; if(isset($photo)) { $widthThumb = 117; $widthImage = 350; $newNamePhoto = uniqid(time()); $newPhoto = "../uploads/".$newNamePhoto; $newNameThumb = $newNameFoto."_thumb.jpg"; $newNameImage = $newNameFoto."_img.jpg"; move_uploaded_file($photo['tmp_name'], $newPhoto); $thumb = imgCreate($newFoto, $newPhoto, $widthThumb); $file = fopen("../uploads/".$newNameThumb,"wb"); fwrite($file, $thumb, sizeof($thumb)); fclose($file); $image = imgCreate($newFoto, $newPhoto, $widthImage); $file = fopen("../uploads/".$newNameImage,"wb"); fwrite($file, $image, sizeof($image)); fclose($file); } ?> Quote Link to comment Share on other sites More sharing options...
Alkimuz Posted August 26, 2010 Share Posted August 26, 2010 i am not sure what is wrong in your code, but i might help you by showing the code i always use to do what you want, it works perfectly: $filename = 'photo'; $file = time().'-'.$_FILES['photo']['name']; if (trim($_FILES[$filename]['tmp_name']) == TRUE) { //photo uploaden $filename = 'photo'; $source = $_FILES[$filename]['tmp_name']; $path= 'photos/big/'; $path2= 'photos/small/'; $destination = $path.$file ; $destination2 = $path2.$file ; photoupload($filename, $source, $destination, $destination2); //photo big resizen $source = $destination; $maxx= '450'; $maxy= '450'; photoresize($source, $maxx, $maxy); //photo small resizen $source = $destination2; $maxx= '75'; $maxy= '75'; photoresize($source, $maxx, $maxy); } $sql="INSERT INTO photo (file) VALUES ('$file')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } and the used functions: function photoupload($filename, $source, $destination, $destination2) { /*START PHOTOUPLOAD*/ // Does the file have the right MIME type? if ($_FILES[$filename]['type'] != 'image/pjpeg' AND $_FILES[$filename]['type'] != 'image/jpeg' AND $_FILES[$filename]['type'] != 'image/gif' AND $_FILES[$filename]['type'] != 'image/png' AND $_FILES[$filename]['type'] != 'image/wbmp') { echo 'the file you want to upload is no photo, please go back to try again'; exit; } move_uploaded_file ($source, $destination ); copy($destination, $destination2); return 0; /*END PHOTOUPLOAD*/ } function photoresize($source, $maxx, $maxy) { /*START PHOTORESIZE*/ // Get current dimensions list($width_orig, $height_orig) = getimagesize($source); // Check if they are over their limit if ( ($width_orig > $maxx) || ( $height_orig > $maxy)) { if ($maxx && ($width_orig < $height_orig)) { $maxx = ($maxy / $height_orig) * $width_orig; } else { $maxy = ($maxx / $width_orig) * $height_orig; } // Resample voorbereiden $image_p = imagecreatetruecolor($maxx, $maxy); $image_type = strtolower( substr($source, strrpos( $source, '.' )) ); switch( $image_type ) { case '.gif' : $image = imagecreatefromgif($source); break; case '.jpg' : $image = imagecreatefromjpeg($source); break; case '.jpeg': $image = imagecreatefromjpeg($source); break; case '.png' : $image = imagecreatefrompng($source); break; case '.bmp' : $image = imagecreatefromwbmp($source); break; } // Resample de afbeelding imagecopyresampled($image_p, $image, 0, 0, 0, 0, $maxx, $maxy, $width_orig, $height_orig); // Output switch( $image_type) { case '.gif' : imagegif($image_p, $source, 100); break; case '.jpg' : imagejpeg($image_p, $source, 100); break; case '.jpeg' : imagejpeg($image_p, $source, 100); break; case '.png' : imagepng($image_p, $source, 100); break; case '.bmp' : image2wbmp($image_p, $source, 100);break; } } return 0; /*END PHOTORESIZE*/ } hope it helps! 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.