Jump to content

GD Library - How to scale imported image


CrimpJiggler

Recommended Posts

I made this tool:

http://chemistryhelper.eu.pn/

with PHP and what I need it to be able to do now is to make some of the lobes grow or shrink. How I made this script is there is one script which loads single lobes from png and arranges them into a row like this:

48d1329c1e66811e70328dad1f974241.png

then a second script loads the first one, and arranges the rows into a stack like this:

340d457e517e99ea0e0bfd03220d3e37.png

 

so I need to figure out how to make single lobes grow or shrink. Heres the script which loads the single lobes:

		mb_language('uni');
		mb_internal_encoding('UTF-8');

		header ("Content-type: image/png"); 
		$handle = ImageCreate ($this->img_width, $this->img_height) or die ("Cannot Create image"); 
		imagesetthickness($handle,3); 

		$lobe['color_one']['positive'] = imagecreatefrompng("$lobe_dir/lobe_u_$colors[0].png");
		$lobe['color_one']['negative'] = imagecreatefrompng("$lobe_dir/lobe_d_$colors[0].png");
		$lobe['color_two']['positive'] = imagecreatefrompng("$lobe_dir/lobe_u_$colors[1].png");
		$lobe['color_two']['negative'] = imagecreatefrompng("$lobe_dir/lobe_d_$colors[1].png");

		$bg_color = ImageColorAllocate ($handle, 255, 255, 255); 
		$line_color = ImageColorAllocate ($handle, 0, 0, 0); 




ImageLine($handle, $line_coords['x']['start'], $line_y_position, $line_coords['x']['end'], $line_y_position, $line_color);


		// merge the two images

		$lobe_y_offset = 28; 

		$height = $line_y_position - $lobe_y_offset;
		$low = $line_y_position + 2;
			
		$lobe_start = $line_coords['x']['start'];
		$increment = ($line_coords['x']['width'] / $this->lobe_num);


		for ($i=0; $i<$this->lobe_num; $i++) {
			
			$lobe_x_position = $lobe_start + ( $increment * $i ) + ( $increment / 4.2 );
			imagecopy($handle, $top_lobe, $lobe_x_position, $height, 0, 0, imagesx($top_lobe), imagesy($top_lobe)); 
			imagecopy($handle, $bottom_lobe, $lobe_x_position, $low, 0, 0, imagesx($bottom_lobe), imagesy($bottom_lobe)); 

		}

I removed a lot of code to make it quick to read, thats basically the part of the code which makes the straight line and adds 2 rows of lobes above and below it. To scale the lobes, I tried this:

		$percent = 0.5;

		// Get new sizes
		list($width, $height) = getimagesize("$lobe_dir/lobe_u_$colors[0].png");
		$newwidth = $width * $percent;
		$newheight = $height * $percent;

		// Load
		$thumb = imagecreatetruecolor($newwidth, $newheight);
        
		$lobe['color_one']['positive'] = imagecreatefrompng("$lobe_dir/lobe_u_$colors[0].png");
		$lobe['color_one']['negative'] = imagecreatefrompng("$lobe_dir/lobe_d_$colors[0].png");
		$lobe['color_two']['positive'] = imagecreatefrompng("$lobe_dir/lobe_u_$colors[1].png");
		$lobe['color_two']['negative'] = imagecreatefrompng("$lobe_dir/lobe_d_$colors[1].png");

		imagecopyresized($thumb, $lobe['color_one']['positive'], 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

that should shrink one of the type of lobes to half its size, but it doesn't work, it doesn't change the size at all. 

Edited by CrimpJiggler
Link to comment
Share on other sites

I figured out the first problem, so heres my second issue. I need to make the backgrounds of the lobes transparent,  so I added this code:

        imagealphablending($lobe['green']['negative'], true); // setting alpha blending on
        imagesavealpha($lobe['green']['negative'], true); 

and the problem is when I use a lobe with a transparent background, bugs start happening like this:

0eba9d737c07930405b094f78c106d9f.png

those lobes aren't supposed to be gray. I have no idea how that can even happen. 

Link to comment
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.