|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-11-08 01:16 UTC] jagja870 at school dot lu
Description:
------------
if you create an image source from an image file, with imagecreatefrompng($path)
$path leads to a png image
you will come to a bug, where if u use imagecolorat() will always return an rgb value of 0.
but if you change it to imgcreatefromstring($path) and load the png file it also won't work.
imagecolorat() only works with jpg files in the end.
Test script:
---------------
$imageSource = file_get_contents("uploads/"."myfile.png");
$imageSource = imagecreatefrompng($imageSource);
for($x=0; $x<$imageX;$x++){
for($y=0;$y<$imageY;$y++){
$rgb = imagecolorat($imageSource, $x, $y);
}
}
Expected result:
----------------
All returns get a value of 0.
Actual result:
--------------
The actual result should have returned the rgb value of the pixel in a png file.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 00:00:01 2025 UTC |
Full Code working: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Trier les couleurs</title> <link rel="stylesheet" href="design.css"> </head> <body> <?php //echo 'Please wait...'; //echo 'Uploading Files...'; // Count # of uploaded files in array $total = count($_FILES['upload']['name']); //echo 'Clearing Directories....'; $files = glob('uploads*'); // get all file names foreach($files as $file){ // iterate files if(is_file($file)) unlink($file); // delete file } // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploads/" . $_FILES['upload']['name'][$i]; $newFilePath = substr($newFilePath, 0 , strlen($newFilePath)-4); $newFilePath.=".jpg"; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } //echo 'Collecting Colors'; $output="<table><th>Images</th><th>R</th><th>G</th><th>B</th><th>RGB divider par 3</th>"; $fileList= scandir("uploads"); $list=[array("Image","R","G","B","RGB divider par 3")]; $ImageList=[]; foreach ($fileList as $file){ if($file != "." && $file != ".."){ $imagePath ="uploads/".$file; echo "Processing: $file<br>"; $imageSource = file_get_contents("uploads/".$file); $imageSource = imagecreatefromstring($imageSource); $size = getimagesize($imagePath); $imageX = $size[0]; $imageY = $size[1]; $imageResult=[]; $imageResult['R']=0; $imageResult['G']=0; $imageResult['B']=0; $imageResult['total']=0; $imageResultat=[]; for($x=0; $x<$imageX;$x++){ for($y=0;$y<$imageY;$y++){ $rgb = imagecolorat($imageSource, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $imageResult['R']+=$r; $imageResult['G']+=$g; $imageResult['B']+=$b; $imageResult['total'] += $r+$g+$b; } } $imageResult['R']=$imageResult['R']/($imageX*$imageY); $imageResult['G']=$imageResult['G']/($imageX*$imageY); $imageResult['B']=$imageResult['B']/($imageX*$imageY); $imageResult['total']=$imageResult['total']/3; // echo print_r($imageResult, true); $list[] = array($file,$imageResult['R'],$imageResult['G'],$imageResult['B'],$imageResult['total']); $output.="<tr><td>$file</td><td>{$imageResult['R']}</td><td>{$imageResult['G']}</td><td>{$imageResult['B']}</td><td>${imageResult['total']}</td></tr>"; unlink("uploads/$file"); } } $files = glob('uploads*'); // get all file names foreach($files as $file){ // iterate files if(is_file($file)) unlink("uploads/".$file); // delete file } $fp = fopen('Result.csv', 'w'); foreach ($list as $fields) { fputcsv($fp, $fields); } fclose($fp); echo "<hr>"; echo '<p>Finished Processing files!</p><br>'; echo '<p>Get Excel file: <a href="'."Result.csv".'">Download</a></p>'; echo "<hr>"; echo $output; ?> </body> </html> ----------------------- Code not working: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Trier les couleurs</title> <link rel="stylesheet" href="design.css"> </head> <body> <?php //echo 'Please wait...'; //echo 'Uploading Files...'; // Count # of uploaded files in array $total = count($_FILES['upload']['name']); //echo 'Clearing Directories....'; $files = glob('uploads*'); // get all file names foreach($files as $file){ // iterate files if(is_file($file)) unlink($file); // delete file } // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploads/" . $_FILES['upload']['name'][$i]; $newFilePath = substr($newFilePath, 0 , strlen($newFilePath)-4); $newFilePath.=".jpg"; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } //echo 'Collecting Colors'; $output="<table><th>Images</th><th>R</th><th>G</th><th>B</th><th>RGB divider par 3</th>"; $fileList= scandir("uploads"); $list=[array("Image","R","G","B","RGB divider par 3")]; $ImageList=[]; foreach ($fileList as $file){ if($file != "." && $file != ".."){ $imagePath ="uploads/".$file; echo "Processing: $file<br>"; $imageSource = file_get_contents("uploads/".$file); $imageSource = imagecreatefrompng($imageSource); $size = getimagesize($imagePath); $imageX = $size[0]; $imageY = $size[1]; $imageResult=[]; $imageResult['R']=0; $imageResult['G']=0; $imageResult['B']=0; $imageResult['total']=0; $imageResultat=[]; for($x=0; $x<$imageX;$x++){ for($y=0;$y<$imageY;$y++){ $rgb = imagecolorat($imageSource, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $imageResult['R']+=$r; $imageResult['G']+=$g; $imageResult['B']+=$b; $imageResult['total'] += $r+$g+$b; } } $imageResult['R']=$imageResult['R']/($imageX*$imageY); $imageResult['G']=$imageResult['G']/($imageX*$imageY); $imageResult['B']=$imageResult['B']/($imageX*$imageY); $imageResult['total']=$imageResult['total']/3; // echo print_r($imageResult, true); $list[] = array($file,$imageResult['R'],$imageResult['G'],$imageResult['B'],$imageResult['total']); $output.="<tr><td>$file</td><td>{$imageResult['R']}</td><td>{$imageResult['G']}</td><td>{$imageResult['B']}</td><td>${imageResult['total']}</td></tr>"; unlink("uploads/$file"); } } $files = glob('uploads*'); // get all file names foreach($files as $file){ // iterate files if(is_file($file)) unlink("uploads/".$file); // delete file } $fp = fopen('Result.csv', 'w'); foreach ($list as $fields) { fputcsv($fp, $fields); } fclose($fp); echo "<hr>"; echo '<p>Finished Processing files!</p><br>'; echo '<p>Get Excel file: <a href="'."Result.csv".'">Download</a></p>'; echo "<hr>"; echo $output; ?> </body> </html> ------------------------------- The code which works, also only works then with jpg / jpeg Kind regards