PHP - Place an image over another image

12,712

to do this kind of work, you need to use GD library or ImageMagic

this code work with GD library

       $photo_to_paste="image_to_paste.jpg";  //image 321 x 400
       $white_image="white_image.jpg"; //873 x 622 

        $im = imagecreatefromjpeg($white_image);
        $condicion = GetImageSize($photo_to_paste); // image format?

        if($condicion[2] == 1) //gif
        $im2 = imagecreatefromgif("$photo_to_paste");
        if($condicion[2] == 2) //jpg
        $im2 = imagecreatefromjpeg("$photo_to_paste");
        if($condicion[2] == 3) //png
        $im2 = imagecreatefrompng("$photo_to_paste");

        imagecopy($im, $im2, (imagesx($im)/2)-(imagesx($im2)/2), (imagesy($im)/2)-(imagesy($im2)/2), 0, 0, imagesx($im2), imagesy($im2));

        imagejpeg($im,"test4.jpg",90);
        imagedestroy($im);
        imagedestroy($im2);

that code will output: imagr

Share:
12,712

Related videos on Youtube

Gijo Varghese
Author by

Gijo Varghese

Updated on October 14, 2022

Comments

  • Gijo Varghese
    Gijo Varghese over 1 year

    I want to place an image over another image using php. I have two images. One is a photo. And other one is a image, of fixed size, full white. We can call this as frame. So what i need is place the photo in the middle of the frame (white image) and then save it. Can anyone pls help me?

    • Ankur Aggarwal
      Ankur Aggarwal about 10 years
      That is the job of CSS not php :/
    • Gijo Varghese
      Gijo Varghese about 10 years
      @AnkurAggarwal yeah i know about that css. But in my case i have to do it in PHP
    • Gijo Varghese
      Gijo Varghese about 10 years
      @shashank yeah i know about that css. But in my case i have to do it in PHP
  • Gijo Varghese
    Gijo Varghese about 10 years
    I cannot use css here. I need PHP