lewisstevens1 Posted September 2, 2013 Share Posted September 2, 2013 Hello i am just wondering about this code - its strange... when i just work with one section it works... but thing is i want to create two images from the one image in the /temp/ directory. This will then have two sent to /small/ and /large/ but it just comes up blank with no error... yet it only happens with larger image file sizes and .JPG extensions instead of .jpg.... The strange thing is... if i just work with one section of the code... it works! ...whatever the image. Hope you can help. Thanks Lewis $image_name = 'image'; $product_src1 = 'plus_cms/image_uploads/product_images/temp/'.$image_name.'.jpg'; $product_src2 = 'plus_cms/image_uploads/product_images/temp/'.$image_name.'.jpg'; $product_src3 = 'plus_cms/image_uploads/product_images/small/'.$image_name.'.jpg'; $product_src4 = 'plus_cms/image_uploads/product_images/large/'.$image_name.'.jpg'; $targ_w1 = $get_width; $targ_h1 = $get_height; $im1 = imagecreatetruecolor($targ_w1, $targ_h1) or die('Cannot Initialize new GD image stream'); // Background to Image $img_r1 = imagecreatefromjpeg($product_src1); //Creating Image $image1 = imagecopyresampled($im1,$img_r1,0,0,$_POST['x'],$_POST['y'],$targ_w1,$targ_h1,$_POST['w'],$_POST['h']); imagejpeg($im1, $product_src3); $targ_w2 = $get_width * 2; $targ_h2 = $get_height * 2; $im2 = imagecreatetruecolor($targ_w2, $targ_h2) or die('Cannot Initialize new GD image stream'); $img_r2 = imagecreatefromjpeg($product_src2); $image2 = imagecopyresampled($im2,$img_r2,0,0,$_POST['x'],$_POST['y'],$targ_w2,$targ_h2,$_POST['w'],$_POST['h']); imagejpeg($im2, $product_src4); Quote Link to comment Share on other sites More sharing options...
lewisstevens1 Posted September 3, 2013 Author Share Posted September 3, 2013 Anyone? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 3, 2013 Share Posted September 3, 2013 You've provided incomplete code. There are variable that have not been defined that are being referenced. $targ_w2 = $get_width * 2; $targ_h2 = $get_height * 2; Where are $get_width and $ defined? Also, I see no purpose for creating multiple instances of the same source image. Quote Link to comment Share on other sites More sharing options...
lewisstevens1 Posted September 3, 2013 Author Share Posted September 3, 2013 (edited) this was only a snippet of code and why would i need to define $? $get_width = $third_url[2]; $get_height = $third_url[3]; these are defined alot earlier in the script and come from $third_url = preg_split('/[-_.]/', $_COOKIE['image_id']); We need to create multiple instances as for some reason it wouldn't work referencing from the same source image :/ Here's the full code of that script. <?php include_once('../../connection/db_connect.php'); $product_id = $_COOKIE['image_id']; $third_url = preg_split('/[-_.]/', $product_id); $third_url1 = $third_url[0]; $image_name = $third_url[1]; $get_width = $third_url[2]; $get_height = $third_url[3]; $r_text = "Recommended Size:<br/>Width: ".$get_width."px - Height: ".$get_height."px"; $layout_src1 = 'plus_cms/image_uploads/layout_uploads/temp/'.$image_name.'.jpg'; $layout_src2 = 'plus_cms/image_uploads/layout_uploads/cropped_images/'.$image_name.'.jpg'; $product_src1 = 'plus_cms/image_uploads/product_images/temp/'.$image_name.'.jpg'; $product_src2 = 'plus_cms/image_uploads/product_images/temp/'.$image_name.'.jpg'; $product_src3 = 'plus_cms/image_uploads/product_images/small/'.$image_name.'.jpg'; $product_src4 = 'plus_cms/image_uploads/product_images/large/'.$image_name.'.jpg'; if (isset($_POST['change_image'])){ echo "<script type='text/javascript'> function clear_url(val){ var val1 = val.replace('?view=crop_image',''); var val2 = val1.replace('&view=crop_image',''); var val3 = val2.replace('?set_cookie=1',''); var val4 = val3.replace('&set_cookie=1',''); return val4; } var geturl = location.href; var geturl2 = clear_url(geturl) var geturl3 = geturl2 + '?set_cookie=1'; window.location.href = geturl3; </script>"; } if (isset($_POST['image2'])){ if($third_url1 === "layout"){ $targ_w = $get_width; $targ_h = $get_height; $im = @imagecreatetruecolor($targ_w, $targ_h) or die('Cannot Initialize new GD image stream'); $img_r = imagecreatefromjpeg($layout_src1); $image = imagecopyresampled($im,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']); imagejpeg($im, $layout_src2); unlink($layout_src1); } else if($third_url1 === "products"){ $targ_w1 = $get_width; $targ_h1 = $get_height; $im1 = imagecreatetruecolor($targ_w1, $targ_h1) or die('Cannot Initialize new GD image stream'); // Background to Image $img_r1 = imagecreatefromjpeg($product_src1); //Creating Image $image1 = imagecopyresampled($im1,$img_r1,0,0,$_POST['x'],$_POST['y'],$targ_w1,$targ_h1,$_POST['w'],$_POST['h']); imagejpeg($im1, $product_src3); //unlink($product_src1); //unlink($product_src2); $targ_w2 = $get_width * 2; $targ_h2 = $get_height * 2; $im2 = imagecreatetruecolor($targ_w2, $targ_h2) or die('Cannot Initialize new GD image stream'); $img_r2 = imagecreatefromjpeg($product_src2); $image2 = imagecopyresampled($im2,$img_r2,0,0,$_POST['x'],$_POST['y'],$targ_w2,$targ_h2,$_POST['w'],$_POST['h']); imagejpeg($im2, $product_src4); } } ?> Edited September 3, 2013 by lewisstevens1 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 3, 2013 Share Posted September 3, 2013 image manipulation requires a huge amount of memory and processing time because you must perform operations on the uncompressed image, pixel by pixel. if you had php's error_reporting set to E_ALL and display_errors set to ON, you would likely be getting errors alerting you to where and what is failing. after you complete the processing for any one image, you need to call imagedestroy( ... ); for each of the three image resources you have created to free up the memory used by them (or re-use the same variables as there's no reason to have code with variables like $im1, $im2...) Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 3, 2013 Share Posted September 3, 2013 . . . why would i need to define $? That was a copy/paste goof, should have been $get_height Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted September 3, 2013 Share Posted September 3, 2013 (edited) If you are generating the file multiple times, you'll need a separate imagejpeg( ) for the image stream. Also, you would want the image stream for display on the last iteration of the image file. } else if($third_url1 === "products"){ $targ_w1 = $get_width; $targ_h1 = $get_height; $im1 = imagecreatetruecolor($targ_w1, $targ_h1) or die('Cannot Initialize new GD image stream'); // Background to Image $img_r1 = imagecreatefromjpeg($product_src1); //Creating Image $image1 = imagecopyresampled($im1,$img_r1,0,0,$_POST['x'],$_POST['y'],$targ_w1,$targ_h1,$_POST['w'],$_POST['h']); //here you created a file src3, no output here imagejpeg($im1, $product_src3); imagedestroy($im1); //unlink($product_src1); //unlink($product_src2); $targ_w2 = $get_width * 2; $targ_h2 = $get_height * 2; $im2 = imagecreatetruecolor($targ_w2, $targ_h2) or die('Cannot Initialize new GD image stream'); $img_r2 = imagecreatefromjpeg($product_src2); $image2 = imagecopyresampled($im2,$img_r2,0,0,$_POST['x'],$_POST['y'],$targ_w2,$targ_h2,$_POST['w'],$_POST['h']); //here you created a file with src4, no output here imagejpeg($im2, $product_src4); //now image stream to browser window imagejpeg($im2, NULL); imagedestroy($im2); } Try that. The issue has to do with headers and when they are being sent to the browser window. Your file is likely correct, its just not sending and image stream to the display. Edited September 3, 2013 by rwhite35 Quote Link to comment Share on other sites More sharing options...
lewisstevens1 Posted September 4, 2013 Author Share Posted September 4, 2013 imagejpeg($im1, $product_src3); // The file output is here.... The second parameter means the file output location... it doesn't display the image( as if it would if you put NULL and headers)... but it just means its a file creation output. Quote Link to comment Share on other sites More sharing options...
Solution lewisstevens1 Posted September 4, 2013 Author Solution Share Posted September 4, 2013 Well i did a work around - i had to make it forward to another php page for further processing then redirect back.. not great... but it works. Also thankyou i forgot about the imagedestroy().. i was just stupidly wasting memory. 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.