Jump to content

Php and Gd Images Simple Manipulation


ghostrider.k9

Recommended Posts

Hi Ppl really need your help cos i am stuck here. I need to resize and image and merge it with another image in my folder. i need your help..

 

<?php

//Function for unidentified extensions of images

function open_image ($file) {

        $extension = strrchr($file, '.');

        $extension = strtolower($extension);

        switch($extension) {

case '.jpg':

case '.jpeg':

$im = @imagecreatefromjpeg($file);

break;

case '.gif':

$im = @imagecreatefromgif($file);

break;

default:

$im = false;

break;

        }

        return $im;

}

 

// Load image

$image = open_image('test.jpg');

if ($image === false) { die ('Unable to open image'); }

 

// Get original width and height

$width = imagesx($image);

$height = imagesy($image);

 

$aspect_ratio=round($height/$width,2);

 

header('Content-type: image/jpeg');

//Width is fixed for images where width is more than height

$new_width = 300;

//New Height

$new_height = round((300/$width)*$height,0);

//shrink the image with the new Height

$image_resized = imagecreatetruecolor($new_width, $new_height);

 

//Create the White Border

$horz = imagecreatefromjpeg("images/whiteborder.jpg");

//Create the Second Image

$vert = imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//Get the height of the resized Image

$height_resized = imagesy($vert);

//Merge the Pixels

imagecopymerge($horz, $vert, 1, 113-($height_resized/2), 0, 0, 298, $height_resized, 100);

//Create the Converted Image

imagejpeg($horz);

//Destroy The Images

imagedestroy($horz);

imagedestroy($vert);

?>

thanks..

 

Link to comment
Share on other sites

I got the solution but i need to save the result image into my folder How to??

 

<?php

header("Content-type: image/jpeg");

function resizeimg($image)

{

  $x = @getimagesize($image);

  $width = $x[0];

  $height = $x[1];

 

  $new_width = round((225/$height)*$width,0);

  $new_height = round((300/$width)*$height,0);

 

  $new = @ImageCreateFromJPEG($image) or $new = @ImageCreateFromPNG($image) or $new =

      @ImageCreateFromGIF($image) or $new = false;

 

  if(!$new)

  {

      readfile($new);

  }

  else

  {

      $thumb = @ImageCreateTrueColor($new_width, $new_height);

      @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

      return $thumb;

  }

  return false;

}

$image = resizeimg('test4.jpg');

$horz = imagecreatefromjpeg("images/whiteborder.jpg");

$height_resized = imagesy($image);

$width_resized = imagesx($image);

imagecopymerge($horz, $image, 150-($width_resized/2), 113-($height_resized/2), 0, 0, $width_resized, $height_resized, 100);

imagejpeg($horz);

imagedestroy($horz);

imagedestroy($vert);

 

?>

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.