Asked  7 Months ago    Answers:  5   Viewed   34 times

I really cannot find a way to successfully do it.. I've searched google for this and it either has black shades around the images or all the images don't overlap. Could you please help?

I am alright at PHP; I'd give myself a 2/5.. I would really appreciate if someone would be willing to help me out.

I'm looking for a simple api that goes something like:

$color=$_GET['color'];
$face=$_GET['face'];
$hat=$_GET['hat'];

echo '<img src="avatar.php?color=$color&face=$face&hat=$hat">';

Thanks for any help in advance. I can understand php from my knowledge of other languages, too, so don't be afraid to talk technical with me; but not too technical.

 Answers

71

there are so many comments on this answer so I'm posting this as an answer.

Got it working on my pc.

use svens code :

    $images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );

    // Allocate new image
    $img = imagecreatetruecolor(58, 75);
    // Make alpha channels work
    imagealphablending($img, true);
    imagesavealpha($img, true);

    foreach($images as $fn) {
        // Load image
        $cur = imagecreatefrompng($fn);
        imagealphablending($cur, true);
        imagesavealpha($cur, true);

        // Copy over image
        imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);

        // Free memory
        imagedestroy($cur);
    }   

    header('Content-Type: image/png');  // Comment out this line to see PHP errors
    imagepng($img);

?>

I renamed your images like this so its easier :


smile : a.png
headset : b.png
blue : c.png

Turns out the problem is with the layering it. Putting one behind the other

after you rename the images, use this url -- it will work(works on my pc).

YOUR_FILE.php?hat=b.png&color=c.png&face=a.png

This will still give you a black background. I am not sure if you have the exact same code as above in your file on the server - because I played around with the image order on your link and it does not help. Try copy-pasting this exact same code on a different file and then trying. Play around with the order and check the results.

Wednesday, March 31, 2021
 
daiscog
answered 7 Months ago
48

By cannibalising a previous answer of mine I've written the following:

<?php

$srcImg = '1.jpg';
$destImg = '2.jpg';
$dstX = 300;
$dstY = 40;
$srcX = 0;
$srcY = 0;
//$srcW = 200; // replaced with $src_width
//$srcH = 226; // replaced with $src_height
$pct = 100;

// create destination image resource.
$dest = imagecreatefromjpeg($destImg);
$dest_width = imagesx($dest);
$dest_height = imagesy($dest);

// create source image resource and define transparent colour.
$src = imagecreatefromjpeg($srcImg);
$src_width = imagesx($src);
$src_height = imagesy($src);
imagecolortransparent($src, imagecolorallocate($src, 255, 0, 255));

// create a circular mask and use it to crop the source image.
$mask = imagecreatetruecolor($src_width, $src_height);
$black = imagecolorallocate($mask, 0, 0, 0);
$magenta = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $magenta);
$r = min($src_width, $src_height);
imagefilledellipse($mask, ($src_width / 2), ($src_height / 2), $r, $r, $black);
imagecolortransparent($mask, $black);
imagecopymerge($src, $mask, 0, 0, 0, 0, $src_width, $src_height, 100);
imagedestroy($mask);

// merge the two images to create the result.
imagecopymerge($dest, $src, $dstX, $dstY, $srcX, 0, $src_width, $src_height, $pct);

// output.
header('Content-type: image/jpeg');
imagejpeg($dest);
imagedestroy($src);
imagedestroy($dest);

This correctly crops the source image to a circle and merges it with the destination. You'll need to adjust the positioning to your preference.

enter image description here

Saturday, May 29, 2021
 
dkcwd
answered 5 Months ago
82

Summary

The error message

Cannot 'squash' without a previous commit

means you likely attempted to “squash downward.” Git always squashes a newer commit into an older commit or “upward” as viewed on the interactive rebase todo list, that is into a commit on a previous line. Changing the command on your todo list’s very first line to squash will always produce this error as there is nothing for the first commit to squash into.

The Fix

First get back to where you started with

$ git rebase --abort

Say your history is

$ git log --pretty=oneline
a931ac7c808e2471b22b5bd20f0cad046b1c5d0d c
b76d157d507e819d7511132bdb5a80dd421d854f b
df239176e1a2ffac927d8b496ea00d5488481db5 a

That is, a was the first commit, then b, and finally c. After committing c we decide to squash b and c together:

(Note: Running git log pipes its output into a pager, less by default on most platforms. To quit the pager and return to your command prompt, press the q key.)

Running git rebase --interactive HEAD~2 gives you an editor with

pick b76d157 b
pick a931ac7 c

# Rebase df23917..a931ac7 onto df23917
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

(Notice that this todo list is in the reverse order as compared with the output of git log.)

Changing b’s pick to squash will result in the error you saw, but if instead you squash c into b (newer commit into the older or “squashing upward”) by changing the todo list to

pick   b76d157 b
squash a931ac7 c

and save-quitting your editor, you'll get another editor whose contents are

# This is a combination of 2 commits.
# The first commit's message is:

b

# This is the 2nd commit message:

c

When you save and quit, the contents of the edited file become commit message of the new combined commit:

$ git log --pretty=oneline
18fd73d3ce748f2a58d1b566c03dd9dafe0b6b4f b and c
df239176e1a2ffac927d8b496ea00d5488481db5 a

Note About Rewriting History

Interactive rebase rewrites history. Attempting to push to a remote that contains the old history will fail because it is not a fast-forward.

If the branch you rebased is a topic or feature branch in which you are working by yourself, no big deal. Pushing to another repository will require the --force option, or alternatively you may be able, depending on the remote repository’s permissions, to first delete the old branch and then push the rebased version. Examples of those commands that will potentially destroy work is outside the scope of this answer.

Rewriting already-published history on a branch in which you are working with other people without very good reason such as leaking a password or other sensitive details forces work onto your collaborators and is antisocial and will annoy other developers. The “Recovering From an Upstream Rebase” section in the git rebase documentation explains, with added emphasis.

Rebasing (or any other form of rewriting) a branch that others have based work on is a bad idea: anyone downstream of it is forced to manually fix their history. This section explains how to do the fix from the downstream’s point of view. The real fix, however, would be to avoid rebasing the upstream in the first place.

Sunday, June 6, 2021
 
Skipper
answered 5 Months ago
90

If the image it is returning is a valid file, then I would recommend running the scrape twice (ie. download it twice and check to see if they are the same).

Another option would be to check the last few pixels of the image (ie. bottom-right corner) to see if they match that color of grey exactly. If they do, then redownload. (obviously this approach fails if you download an image that is actually supposed to be grey in that corner, in that exact colour...but if you check several of the last pixels it should reduce the chance of that to an acceptable level).

Thursday, July 29, 2021
 
ranhan
answered 3 Months ago
58

I think the solution is simple: remove the return true from the ontouchlistener. Since that blocks all further operations that respond to touch and input. Make it return false too.

This way it will allow other actions to also respond to the touch.

Saturday, October 9, 2021
 
Gordnfreeman
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :