PHP imagecreatetruecolour center image
<p>I am trying to center an image to the middle of an</p>

<pre><code>$thumbnail_gd_image = imagecreatetruecolor(600, 300);
</code></pre>

<p>command. If a user uploads an image that is 100x50 I generate a larger version to 600x300 with a white background. Now at the moment the actual image is placed in the top left corner. I would like this image in the middle of the 600x300 generated image.</p>

<p>On the other side, if a user uploads an image that is larger than 600x300 then I resize down to keep within those parameters.</p>

<p>I am building an image upload/cropping tool however the croppable area is always twice as wide as what it is high so I need the useable cropping area to be within 600x300 for any detailed cropping.</p>

<p>Can this be done? The center of a smaller than 600x300 image?</p>

<p>Thanks</p>

<p>EDIT:</p>

<p>Tried the below code but it didnt like it.</p>

<p>This is my code (Probably not the cleanest but it needs to be semi working quickly).</p>

<pre><code>if($source_image_width &lt; 600 &amp;&amp; $source_image_height &lt; 300){
    $x = (600 / 2) - ($source_image_width / 2);
    $y = (300 / 2) - ($source_image_height / 2);
}else if($source_image_width &gt; 600){
    $x = 0;
    if($source_image_height &lt; 300){
        $y = (300 / 2) - ($source_image_height / 2);
    }else{
        $y = 0; 
    }
}else if($source_image_height &gt; 300){
    if($source_image_width &lt; 600){
        $x = (600 / 2) - ($source_image_width / 2);
    }else{
        $x = 0;
    }
    $y = 0;
}else{
    $x = 0;
    $y = 0;
}
</code></pre>

<p>The above code puts a 400 wide x 800 high image slightly to the left of center (about 100px). The hight on any image works perfectly but just not the width.</p>
