PHP - Image Save Error
Hi
I have a javascript image cropping script which is working well, I then have some php to save the new cropped image. But I can't seem to get it working 100%. When the form is posted the cropped image is not created and I get a "The connection was reset" error. If I comment out the line Code: [Select] $source=imagecreatefromjpeg($image); then I dont get the The connection was reset" error but the image is obviously still not created. Can anyone help? Code: [Select] <?php function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){ list($imagewidth, $imageheight, $imageType) = getimagesize($image); $imageType = 'image/jpeg'; $newImageWidth = ceil($width * $scale); $newImageHeight = ceil($height * $scale); $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); switch($imageType) { case "image/jpeg": case "image/jpg": $source=imagecreatefromjpeg($image); break; } imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height); switch($imageType) { case "image/jpeg": case "image/jpg": imagejpeg($newImage,$thumb_image_name,90); break; } chmod($thumb_image_name, 0777); return $thumb_image_name; } function getHeight($image) { $size = getimagesize($image); $height = $size[1]; return $height; } function getWidth($image) { $size = getimagesize($image); $width = $size[0]; return $width; } $large_image_location = $upload_path.$large_image_name.'.jpg'; $thumb_image_location = $upload_path.$thumb_image_name.'.jpg'; if (isset($_POST["upload_thumbnail"])) { $x1 = $_POST["x1"]; $y1 = $_POST["y1"]; $x2 = $_POST["x2"]; $y2 = $_POST["y2"]; $w = $_POST["w"]; $h = $_POST["h"]; $scale = $thumb_width/$w; $cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale); header("location:index.php"); exit(); } ?> Similar TutorialsThis resizer works fine except I would like it to save the original image AND the thumbnail. Right now its only saving the cropped thumbnail. It's seems simple enough but I've tried several different ways but can't get it to work. :( I would appreciate your help. :) define( 'DESIRED_IMAGE_WIDTH', 150 ); define( 'DESIRED_IMAGE_HEIGHT', 150 ); $source_path = $_FILES[ 'thumb' ][ 'tmp_name' ]; $timestamp = time(); $target = "image_files/".$imagename; move_uploaded_file($source, $target); // // Add file validation code here // list( $source_width, $source_height, $source_type ) = getimagesize( $source_path ); switch ( $source_type ) { case IMAGETYPE_GIF: $source_gdim = imagecreatefromgif( $source_path ); break; case IMAGETYPE_JPEG: $source_gdim = imagecreatefromjpeg( $source_path ); break; case IMAGETYPE_PNG: $source_gdim = imagecreatefrompng( $source_path ); break; } $source_aspect_ratio = $source_width / $source_height; $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT; if ( $source_aspect_ratio > $desired_aspect_ratio ) { // // Triggered when source image is wider // $temp_height = DESIRED_IMAGE_HEIGHT; $temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio ); } else { // // Triggered otherwise (i.e. source image is similar or taller) // $temp_width = DESIRED_IMAGE_WIDTH; $temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio ); } // // Resize the image into a temporary GD image // $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height ); imagecopyresampled( $temp_gdim, $source_gdim, 0, 0, 0, 0, $temp_width, $temp_height, $source_width, $source_height ); // // Copy cropped region from temporary image into the desired GD image // $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2; $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2; $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT ); imagecopy( $desired_gdim, $temp_gdim, 0, 0, $x0, $y0, DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT ); // // Render the image // Alternatively, you can save the image in file-system or database // header( 'Content-type: image/jpeg' ); imagejpeg( $desired_gdim, "image_files/" . $timestamp . $_FILES["thumb"]["name"] ); hi all, i'm trying to save an image after cropping it using jcrop (jquery plugin). i'm currently using a script i found on the internet called ImageManipulation.php (attached). its working fine, but i get a black image if i crop a gif, or png images (works great for jpg images). Code: [Select] $objImage = new ImageManipulation("../products/" . $_SESSION['image']); if ($objImage->imageok) { // get x, y, w, h from hidden inputs that are update by jcrop $objImage->setCrop($_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']); $objImage->resize(150); $objImage->save("../products/" . $_SESSION['image']); } how can i fix this?? if you have a better way of using php with jcrop to save cropped images please share it thanks for the help Hi Guys, Please can someone here help me with a simple browse for an image from the computer and then when the form is submitted it uploads the image and then saves the image url in my DB. my form page is here http://bit.ly/gntCvD Any help will be appreciated. thanks Craig Hi, I am tryng to use the gd functions in php to output an image of a lower quality and save it as another image: Code: [Select] header('Content-Type: image/jpeg'); $im = @imagecreatefromjpeg('SDC10424.JPG'); imagejpeg($im, null, 50);// this line lowers the image quality imagedestroy($im); but the problem is how to save it? I tried file_put_contents, but it didnt work, like fileputcontents("test.jpg", $im); I'm trying to save images from a directory into mine. To get the image I am having to take the email from a database, split it and take whatever it is before the '@' sign and add it to "-S.jpg". I wrote the script and when I echo the variable it shows the correct thing, but when it tries to save it , it is trying to find the image as "script>-S.jpg". It looks like it is taking whatever is after the last '/' which in the variable since I am running javascript it is going to be </script> if you look at my variable $url. Here is the code below. Any help is appreciated. while($rows=mysql_fetch_array($result)){ $email=$rows['email']; $url= "<SCRIPT LANGUAGE=\"javascript\"> var url; var email = \"$email\"; function emailsplit () { var userid = email.split(\"@\"); var url = userid[0]; var imgid = \"http://my.snu.edu/images/idpictures/\" + url + \"-S.jpg\"; return url; } document.write(emailsplit()); </script> "; $img[]= 'http://my.snu.edu/images/idpictures/'.$url.'-S.jpg'; } function save_image($img,$fullpath='basename'){ if($fullpath=='basename'){ $fullpath = basename($img); } $ch = curl_init ($img); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $rawdata=curl_exec($ch); curl_close ($ch); if(file_exists($fullpath)){ unlink($fullpath); } $fp = fopen($fullpath,'x'); fwrite($fp, $rawdata); fclose($fp); } foreach($img as $i){ save_image($i); if(getimagesize(basename($i))){ echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>'; }else{ echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>'; } } Hi all, i need little help in saving and download gif image created by using GD library. code works fine in mozilla and crome but in IE a new new page where the image is created,saved as well as dowloaded remains open and shows this"Action canceled". but the image is stored and downloaded on my system. i just want to close this page automatically when download is complete Code: [Select] <?php ob_start(); ini_set("memory_limit","180M"); $data = explode(",", $_POST['img']); $width = $_POST['wp']; $height = $_POST['ht']; $image=imagecreatetruecolor( $width ,$height ); $background = imagecolorallocate( $image ,0 , 0 , 0 ); //Copy pixels $i = 0; for($x=0; $x<=$width; $x++){ for($y=0; $y<=$height; $y++){ $int = hexdec($data[$i++]); $color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int); imagesetpixel ( $image , $x , $y , $color ); } } //Output image and clean header( "Content-type: image/gif" ); $name = rand(0,9999); $n = $name; $name = "img/".$name.".gif"; ImageGIF($image,$name,100); imagedestroy($image); $path = "http://localhost/madeup/img/"; $fullPath = $path.$n.'.gif'; if ($fd = fopen ($fullPath, "r")) { header( "Content-type: image/gif" ); header("Content-Disposition: attachment; filename=".$n); header("Cache-control: private"); while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); ob_end_flush(); exit; ?> How can i save image just assume that i have "test.jpg" and wish to store the image to directory "directory A" using Curl..how can i do that? thanks in adv My current project requires me to save the number of views an image (banner ad) has appeared on users website. The problem here is that the image will be appearing on several users website and i want to be able to save the number of times it has appeared on my database. I know how to do this if the image was on the same server as the database, but not when the image and database are on completely different servers. This is somewhat like analytic where it tracks the visitors to a page. Does anyone how i can accomplish this? i figured i would need to create a JavaScript code to provide to the user so they can place it on the site but how do i get that code to connect to my database? Hey guys! I have the following script to create an excel file, the thing is that I dont want to be asked if I want to open or save the file when accessing the php file...I just want the php file directly to save the excel file. Heres the code: $filename = "test.xls"; $contents = "testdata1 \ntestdata2 \ntestdata3 \n"; header('Content-type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename='.$filename); echo $contents; Is there a way instead of using the header function, use something like: fopen, fwrite, fclose ? Thanks in advance! Cheers, This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=321638.0 Note: This website source has been copied and I am a newb so explain things in the simplest detail not technical, thanks.
Currently my website looks like this...
http://i.gyazo.com/e...050104f3ec9.png
Now on that screenshot the asterisks image is missing.. how do I fix this?
The image is from a website that got deleted therefore the image doesn't exist.
In the PHP file it said this where the asterisks is...
<p class="info">Please enter your valid username and password to login into your account. Fields marked <?php echo required();?> are required</p>How do I change this website link.. as I cannot see the link. Hey My php function keeps returning an error on a valid image! I get this error: Quote could not make seekable I use this line to get the image size but i get the above error for it:$picturearray = getimagesize($Image); Url being tested : Code: [Select] http://www.fileden.com/files/2010/12/22/3043070/Sidroc.jpg The url is correct and the image does exist - does any one know what can cause this issue ? Hey, when a user want to upload a image this error comes up: Code: [Select] Warning: imagejpeg() [function.imagejpeg]: Unable to open 'upload/thumbs/Forest Flowers.jpg' for writing: Permission denied in /home/oppasser/domains/oppassergezocht.nl/public_html/mijn-account.php on line 195 Warning: fopen(upload/thumbs/Forest Flowers.jpg) [function.fopen]: failed to open stream: No such file or directory in /home/oppasser/domains/oppassergezocht.nl/public_html/mijn-account.php on line 197 Warning: filesize() [function.filesize]: stat failed for upload/thumbs/Forest Flowers.jpg in /home/mijn-account.php on line 198 Warning: fread(): supplied argument is not a valid stream resource in /home/mijn-account.php on line 198 Warning: fclose(): supplied argument is not a valid stream resource in /home/mijn-account.php on line 200 Warning: unlink(upload/thumbs/Forest Flowers.jpg) [function.unlink]: No such file or directory in /home/mijn-account.php on line 201 Code: [Select] imagejpeg($dimg, "$tdir" . $url); // Saving The Image $tmpName = "$tdir" . $url; $fp = fopen($tmpName, 'r'); $updimgcontent = fread($fp, filesize($tmpName)); $updimgcontent = addslashes($updimgcontent); fclose($fp); unlink($tmpName); imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image Help? ok so ive found what looks to be a decent image uploader. which if anyone is interested can be found here; http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ I have followed the instruction and added the few bits needed to make it work properly. However hence me beening here, it doesnt work. Im not overly familiar with the syntax of -> so maybe I am outputting the wrong name in the move_upload_file but the error I get on the server is Unable to move '/tmp/phpJnOkXP' to bla bla bla upload-new.php on line 126 Upload fail which is the move_upload line Here is my code <?php if( isset($_POST['uploadbtn']) ) { $folder = "{$_SERVER['DOCUMENT_ROOT']}/images/test/"; $fileName = $_FILES['newImage']['name']; $tmpName = $_FILES['image']['tmp_name']; include('../simple-image.php'); $image = new SimpleImage(); $image->load($tmpName); $image->resizeToWidth(600); $image->output(); $uploaded = move_uploaded_file($tmpName , $folder.$fileName); if($uploaded){ echo "Upload success"; } else { echo "Upload fail"; } } ?> <form action="" method="post" enctype="multipart/form-data" name="uploadfrm"> <input name="image" type="file" /> <input name="uploadbtn" value="Upload" type="submit" /> </form> Hi Guys, I have an issue with the following script which is throwing this error Quote( ! ) Parse error: syntax error, unexpected end of file in C:\wamp64\www\script\images.php on line 41 I just can not see the error, can anyone help (not even sure the script is going to work) <?php /* settings */ //folder for images saving $saveDir = "c:\wamp64\www\script\images\"; //database connection // Turn off all error reporting error_reporting(0); $conn = mysqli_connect('localhost', 'root', '', 'tbl_temp_products'); //start and end table row to prevent timeout or hard server work $start = '1'; $end = '200'; /* end of settings */ //query for fetching the image urls $sql = 'SELECT image_url FROM tbl_temp_products ORDER BY id DESC LIMIT ' . $start . ',' . $end . ''; $res = $conn->query($sql); $count = 0; //this is for count the total row fetched $notify = ''; //this is for seeing result or errors while ($row = $res->fetch_assoc()) { $url = $row(['image_url']); $dest = $saveDir . clean($row(['image_url'])) . '.jpg'; if (!file_exists($dest)){ //prevent file overwriting if (copy($url, $dest)){ $notify.= 'image saved: '. $row(['image_url']); $count++; }else{ $notify.= 'ERROR saving image: '. $row(['image_url']); }else{ $notify.= 'image already exists: '. $row(['image_url']); } } //output result echo 'TOTAL IMAGE SAVED: ' .$count .'\n'; echo $notify; ?>
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); } ?>
An image is saved in the root directory. $file = '../image.jpg'; // IN ROOT DIRECTORY $type = 'image/jpeg'; header('Content-Type:'.$type); header('Content-Length: ' . filesize($file)); readfile($file);
The image then gets embedded into an html page and displayed like this:
QUESTION: in the event of an error, how do I dipslay the readfile error on the customer's html page? Make sense? No. I probably have to rewrite my question 😃 Edited Monday at 01:07 AM by ChenXiu |