Jump to content

Go to solution Solved by lewisstevens1,

Recommended Posts

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);
Link to comment
https://forums.phpfreaks.com/topic/281791-php-image/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/281791-php-image/#findComment-1447999
Share on other sites

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 by lewisstevens1
Link to comment
https://forums.phpfreaks.com/topic/281791-php-image/#findComment-1448006
Share on other sites

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...)

Link to comment
https://forums.phpfreaks.com/topic/281791-php-image/#findComment-1448032
Share on other sites

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 by rwhite35
Link to comment
https://forums.phpfreaks.com/topic/281791-php-image/#findComment-1448051
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.