Coder Social home page Coder Social logo

Underlay or Border about simpleimage HOT 8 CLOSED

claviska avatar claviska commented on June 14, 2024
Underlay or Border

from simpleimage.

Comments (8)

expertlearner avatar expertlearner commented on June 14, 2024

I've reading about imagefilledrectangle, maybe it's useful for this feature request?

from simpleimage.

nazar-pc avatar nazar-pc commented on June 14, 2024

Sure, looks like you need to create filled rectangle with desired size, then fit image to the same size, and put resized image on filled underlay.

from simpleimage.

claviska avatar claviska commented on June 14, 2024

Look at the create() and overlay() methods.

You can best_fit() your original image, then create() a fixed-size image with a white background and overlay() the original on top of it. I believe the default position is centered.

from simpleimage.

expertlearner avatar expertlearner commented on June 14, 2024

Well, I thought exactly what you just said, best_fit, new white and then overlay, but I found that the overlay requires .png 24 image, and then I thought that maybe it's easy to use imagefilledrectangle.

I tried overlay($img,....) but I got error "SimpleImage" class can not be used as String, so, I need first to output the image? as .png? so it can be used as overlay?

from simpleimage.

claviska avatar claviska commented on June 14, 2024

Put this in the example directory and run it:

<?php
namespace abeautifulsite;
use Exception;

require '../src/abeautifulsite/SimpleImage.php';

if (!is_dir('processed/')) {
    mkdir('processed/');
}

try {

    // Whatever size you like...
    $max_width = 100;
    $max_height = 200;

    // Create the background image in a temp file
    $temp_file = tempnam(sys_get_temp_dir(), 'IMG');
    $background = new SimpleImage();
    $background->create($max_width, $max_height, '#fff')->save($temp_file);

    // Resize your image as usual
    $your_image = new SimpleImage('butterfly.jpg');
    $your_image->best_fit($max_width, $max_height)->save('processed/your-image.jpg');

    // Overlay your image on top of the background image
    $background->overlay('processed/your-image.jpg')->save('processed/your-image.jpg');

    unlink($temp_file);

} catch (Exception $e) {
    echo '<span style="color: red;">'.$e->getMessage().'</span>';
}

So, it's possible out of the box but it currently requires using a temp file. We could avoid this by modifying the overlay() method to accept both filenames and SimpleImage objects.

from simpleimage.

claviska avatar claviska commented on June 14, 2024

Resulting image (with orange background so you can see it on this page):

your-image

from simpleimage.

claviska avatar claviska commented on June 14, 2024

Even easier with e72906f. The overlay() method now accepts a filename or a SimpleImage object:

<?php
namespace abeautifulsite;
use Exception;

require '../src/abeautifulsite/SimpleImage.php';

if (!is_dir('processed/')) {
    mkdir('processed/');
}

try {

    // Whatever size you like...
    $max_width = 100;
    $max_height = 200;

    // Create the background image
    $background = new SimpleImage();
    $background->create($max_width, $max_height, '#fc0');

    // Resize your image as usual
    $your_image = new SimpleImage('butterfly.jpg');
    $your_image->best_fit($max_width, $max_height);

    // Overlay your image on top of the background image
    $background->overlay($your_image)->save('processed/your-image.jpg');

} catch (Exception $e) {
    echo '<span style="color: red;">'.$e->getMessage().'</span>';
}

@nazar-pc If that update looks good to you I'll tag it as a new version tomorrow.

from simpleimage.

expertlearner avatar expertlearner commented on June 14, 2024

Wow, this is awesome, thank you very much !!!

from simpleimage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.