-1

I have this code:

<?php
// EXAMPLE PICTURE
$url='https://admin2.e-hjemmeside.dk/upload/images/89.jpg';

//var_dump(getColorPallet($url));

echoColors(getColorPallet($url));


function echoColors($pallet){ // OUTPUT COLORSBAR
    foreach ($pallet as $key=>$val)
        echo '<div style="display:inline-block;width:50px;height:20px;background:#'.$val.'"> </div>';
}

function getColorPallet($imageURL, $palletSize=[3,3]){ // GET PALLET FROM IMAGE PLAY WITH INPUT PALLET SIZE
    // SIMPLE CHECK INPUT VALUES
    if(!$imageURL) return false;

    // IN THIS EXEMPLE WE CREATE PALLET FROM JPG IMAGE
    $img = imagecreatefromjpeg($imageURL);

    // SCALE DOWN IMAGE
    $imgSizes=getimagesize($imageURL);

    $resizedImg=imagecreatetruecolor($palletSize[0],$palletSize[1]);

    imagecopyresized($resizedImg, $img , 0, 0 , 0, 0, $palletSize[0], $palletSize[1], $imgSizes[0], $imgSizes[1]);

    imagedestroy($img);

    //CHECK IMAGE
    /*header("Content-type: image/png");
    imagepng($resizedImg);
    die();*/

    //GET COLORS IN ARRAY
    $colors=[];

    for($i=0;$i<$palletSize[1];$i++)
        for($j=0;$j<$palletSize[0];$j++)
            $colors[]=dechex(imagecolorat($resizedImg,$j,$i));

    imagedestroy($resizedImg);

    //REMOVE DUPLICATES
    $colors= array_unique($colors);

    return $colors;

}
?>

from this question Detect main colors in an image with PHP

I want to set a value inside the function, like

$colorsinimage = "".$colorsinimage."#".$val."";

to store all the colors in the image as a string. This string can be used outside the function to store into database, echo the string, sort the colors ...

How do I do that?

Thanks in advance :-)

EDIT

Thanks for your comments :-) The link to question .../16959576/.. was very helpful to understand it all. After that I find $GLOBALS og it seems to work in a way. If I ad $GLOBALS['imagecolors'] = ""; first in the code, and $GLOBALS['imagecolors'] = "".$GLOBALS['imagecolors']."#".$val.""; in the first function, I end up to echo $imagecolors; at the end, where the result is the latest color #291a17. Instead of the latest color, I want to set $GLOBALS['imagecolors'] to a string with all the colors, an array of all the different values of $val.

New code with the $GLOBALS:

<?php
$GLOBALS['imagecolors'] = "";
// EXAMPLE PICTURE
$url='https://admin2.e-hjemmeside.dk/upload/images/89.jpg';

//var_dump(getColorPallet($url));

echoColors(getColorPallet($url));


function echoColors($pallet){ // OUTPUT COLORSBAR
    foreach ($pallet as $key=>$val)
        echo '<div style="display:inline-block;width:50px;height:20px;background:#'.$val.'"> </div>';
        $GLOBALS['imagecolors'] = "".$GLOBALS['imagecolors']."#".$val."";
}

function getColorPallet($imageURL, $palletSize=[3,3]){ // GET PALLET FROM IMAGE PLAY WITH INPUT PALLET SIZE

    // SIMPLE CHECK INPUT VALUES
    if(!$imageURL) return false;

    // IN THIS EXEMPLE WE CREATE PALLET FROM JPG IMAGE
    $img = imagecreatefromjpeg($imageURL);

    // SCALE DOWN IMAGE
    $imgSizes=getimagesize($imageURL);

    $resizedImg=imagecreatetruecolor($palletSize[0],$palletSize[1]);

    imagecopyresized($resizedImg, $img , 0, 0 , 0, 0, $palletSize[0], $palletSize[1], $imgSizes[0], $imgSizes[1]);

    imagedestroy($img);

    //CHECK IMAGE
    /*header("Content-type: image/png");
    imagepng($resizedImg);
    die();*/

    //GET COLORS IN ARRAY
    $colors=[];

    for($i=0;$i<$palletSize[1];$i++)
        for($j=0;$j<$palletSize[0];$j++)
            $colors[]=dechex(imagecolorat($resizedImg,$j,$i));

    imagedestroy($resizedImg);

    //REMOVE DUPLICATES
    $colors= array_unique($colors);

    return $colors;
}

echo $imagecolors;
?>

Thanks Alex - it is working perfect :-)

Right code

<?php
// EXAMPLE PICTURE
$url='https://admin2.e-hjemmeside.dk/upload/images/89.jpg';

//var_dump(getColorPallet($url));

echoColors(getColorPallet($url));


function echoColors($pallet){
    global $imagecolors;
    $imagecolors = '';
    foreach ($pallet as $key=>$val) {
        echo '<div style="display:inline-block;width:50px;height:20px;background:#'.$val.'"> </div>';
        $imagecolors .= "#".$val;
    }
}

function getColorPallet($imageURL, $palletSize=[3,3]){ // GET PALLET FROM IMAGE PLAY WITH INPUT PALLET SIZE

    // SIMPLE CHECK INPUT VALUES
    if(!$imageURL) return false;

    // IN THIS EXEMPLE WE CREATE PALLET FROM JPG IMAGE
    $img = imagecreatefromjpeg($imageURL);

    // SCALE DOWN IMAGE
    $imgSizes=getimagesize($imageURL);

    $resizedImg=imagecreatetruecolor($palletSize[0],$palletSize[1]);

    imagecopyresized($resizedImg, $img , 0, 0 , 0, 0, $palletSize[0], $palletSize[1], $imgSizes[0], $imgSizes[1]);

    imagedestroy($img);

    //CHECK IMAGE
    /*header("Content-type: image/png");
    imagepng($resizedImg);
    die();*/

    //GET COLORS IN ARRAY
    $colors=[];

    for($i=0;$i<$palletSize[1];$i++)
        for($j=0;$j<$palletSize[0];$j++)
            $colors[]=dechex(imagecolorat($resizedImg,$j,$i));

    imagedestroy($resizedImg);

    //REMOVE DUPLICATES
    $colors= array_unique($colors);

    return $colors;
}

echo $imagecolors;
?>
C. Boutrup
  • 13
  • 6
  • Which function? You posted 2 – IsThisJavascript Aug 09 '18 at 14:12
  • 1
    Read this thread I think it's what you are looking for: https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and – IsThisJavascript Aug 09 '18 at 14:13
  • `$colorsinimage .= "#".$val;` is the shorter version of what you suggested you want to do. But without any real context of how this string should actually look in the end (provide an example of the exact desired result)... its going to be difficult to guess here. You better be off putting them into an array like: `$colorsinimage[] = "#".$val;` .. then `return array($colors,$colorsinimage);` on your function, so you can use those colors how you want, sorting, storing, chopping etc. – IncredibleHat Aug 09 '18 at 14:14
  • what is your current output and what is your desired output? – Alex Aug 09 '18 at 16:23

1 Answers1

0

Main problem in your code is kind typo.

You have no curly braces after foreach, so you get only the last value from array to your var I guess.

Just fixing this you should get some better result:

function echoColors($pallet){ // OUTPUT COLORSBAR
    foreach ($pallet as $key=>$val) {
        echo '<div style="display:inline-block;width:50px;height:20px;background:#'.$val.'"> </div>';
        $GLOBALS['imagecolors'] = "".$GLOBALS['imagecolors']."#".$val."";
    }
}

But here how is better deal with global variables and some other code optimization:

function echoColors($pallet){
    global $imagecolors;
    $imagecolors = '';
    foreach ($pallet as $key=>$val) {
        echo '<div style="display:inline-block;width:50px;height:20px;background:#'.$val.'"> </div>';
        $imagecolors .= "#".$val;
    }
}
Alex
  • 16,739
  • 1
  • 28
  • 51