Getting memory error with GD - php

i'm having trouble with a script i modified, i used this class https://github.com/thenakos/compare-images since i wanted to check if in a determined folder there were only uniques photos.
public function scanDir($d)
{
/*function to find same photos in a dir*/
$tabImg = array();
$bitsList = array();
if(is_dir($d))
{
$dir = opendir($d);
$i = 0;
while($file = readdir($dir))
{
$path_parts = pathinfo($file);
if($file != '.' && $file != '..' && isset($path_parts['extension']) && $path_parts['extension'] == 'jpg')
{
$tabImg[] = $file;
$i++;
}
}
}
$i=0;
foreach($tabImg as $keyA => $imgA)
{
if($i<700) {
if(file_exists($d.$imgA))
{
$i1 = $this->createImage($d.$imgA);
if(!$i1){return false;}
$i1 = $this->resizeImage($i1,$d.$imgA);
imagefilter($i1, IMG_FILTER_GRAYSCALE);
$colorMean1 = $this->colorMeanValue($i1);
$bits1 = $this->bits($colorMean1);
$bitsList[$keyA] = $bits1;
imagedestroy($i1);
$i++;
}
}
}
$bitsListToCompare = $bitsList;
foreach($bitsList as $keyList => $valueList)
{
foreach($bitsListToCompare as $keyListToCompare => $valueListToCompare)
{
if($keyList != $keyListToCompare)
{
$hammeringDistance = 0;
for($b = 0;$b<64;$b++)
{
if($valueList[$b] != $valueListToCompare[$b])
{
$hammeringDistance++;
}
}
if($hammeringDistance < 5)
{
if(isset($arraySame[$tabImg[$keyList]])) $arraySame[$tabImg[$keyList]] = $arraySame[$a[$keyList]].';'.$tabImg[$keyListToCompare]; else $arraySame[$tabImg[$keyList]] = $tabImg[$keyListToCompare];
}
}
}
unset($bitsListToCompare[$keyList]);
}
return $arraySame;
}
i've added this function wich basically returns an array of duplicates images. This way it works fine, i'm checking 700 images. But if i don't limit the number of photos to check, i'm getting an error.
Warning: getimagesize() [function.getimagesize]: Read error!
This error is about the following function ( getimagesize )
private function mimeType($i)
{
/*returns array with mime type and if its jpg or png. Returns false if it isn't jpg or png*/
$mime = getimagesize($i);
$return = array($mime[0],$mime[1]);
switch ($mime['mime'])
{
case 'image/jpeg':
$return[] = 'jpg';
return $return;
case 'image/png':
$return[] = 'png';
return $return;
default:
return false;
}
}
i think it's something about the memory but i don't know how to make it work !
Thanks

As for memory - this line seems suspicious:
$i1 = $this->resizeImage($i1,$d.$imgA);
I don't know what's inside resizeImage() but it could be that it takes one GD resource as first argument, doesn't destroy it and returns another GD resource. Reference to the new resource replaces reference to the old resource that stays in memory. While resource without references to it will be eventually freed by garbage collector, it's not guaranteed it will do it in time.
So I would do:
$i2 = $this->resizeImage($i1,$d.$imgA);
imagedestroy($i1);
But there may be simpler reason. As PHP manual states on getimagesize():
If accessing the filename image is impossible getimagesize() will generate an error of level E_WARNING. On read error, getimagesize() will generate an error of level E_NOTICE.
Then in changelog:
5.2.3 Read errors generated by this function downgraded to E_NOTICE from E_WARNING.
So perhaps some photo has permission issue or something like that?

Related

PHP file_exists With Contents Instead of Name?

Is there a function built into PHP that acts like file_exists, but given file contents instead of the file name?
I need this because I have a site where people can upload an image. The image is stored in a file with a name determined by my program (image_0.png image_1.png image_2.png image_3.png image_4.png ...). I do not want my site to have multiple images with the same contents. This could happen if multiple people found a picture on the internet and all of them uploaded it to my site. I would like to check if there is already a file with the contents of the uploaded file to save on storage.
This is how you can compare exactly two files with PHP:
function compareFiles($file_a, $file_b)
{
if (filesize($file_a) == filesize($file_b))
{
$fp_a = fopen($file_a, 'rb');
$fp_b = fopen($file_b, 'rb');
while (($b = fread($fp_a, 4096)) !== false)
{
$b_b = fread($fp_b, 4096);
if ($b !== $b_b)
{
fclose($fp_a);
fclose($fp_b);
return false;
}
}
fclose($fp_a);
fclose($fp_b);
return true;
}
return false;
}
If you keep the sha1 sum of each file you accept you can simply:
if ($known_sha1 == sha1_file($new_file))
You can use a while loop to look look through the contents of all of your files. This is shown in the example below :
function content_exists($file){
$image = file_get_contents($file);
$counter = 0;
while(file_exists('image_' . $counter . '.png')){
$check = file_get_contents('image_' . $counter . '.png');
if($image === $check){
return true;
}
else{
$counter ++;
}
}
return false;
}
The above function looks through all of your files and checks to see if the given image matches an image that is already stored. If the image already exists, true is returned and if the image does not exist false is returned. An example of how you can use this function shown is below :
if(content_exists($_FILES['file']['tmp_name'])){
// upload
}
else{
// do not upload
}
You could store hashed files in a .txt file separated by a \n so that you could use the function below :
function content_exists($file){
$file = hash('sha256', file_get_contents($file));
$files = explode("\n", rtrim(file_get_contents('files.txt')));
if(in_array($file, $files)){
return true;
}
else{
return false;
}
}
You could then use it to determine whether or not you should save the file as shown below :
if(content_exists($_FILES['file']['tmp_name'])){
// upload
}
else{
// do not upload
}
Just make sure that when a file IS stored, you use the following line of code :
file_put_contents('files.txt', hash('sha256', file_get_contents($file)) . "\n");

How to calculate entire directory size with FTP access using PHP

I have a number of different hosting accounts set up for clients and need to calculate the amount of storage space being used on each account, which would update regularly.
I have a database set up to record each clients storage usage.
I attempted this first using a PHP file on each account, run by a Cron Job. If run manually by myself, it would output the correct filesize and update the correct size to the database, although when run from the Cron Job, it would output 0.
I then attempted to run this file from a Cron Job from the main account but figured this wouldn't actually work as my hosting would block files from another server and I would end up with the same result as before.
I am now playing around with FTP access to each account from a Cron Job from the main account which looks something like below, the only problem is I don't know how to calculate directory size rather than single file sizes using FTP access, and don't know how to reiterate this way? Hoping somebody might be able to help here before I end up going around in circles?
I will also add the previous first attempt too.
$ftp_conn = ftp_connect($ftp_host, 21, 420) or die("Could not connect to server");
$ftp_login = ftp_login($ftp_conn, $ftp_username, 'mypassword');
$total_size = 0;
$contents = ftp_nlist($ftp_conn, ".");
// output $contents
foreach($contents as $folder){
while($search == true){
if($folder == '..' || $folder == '.'){
} else {
$file = $folder;
$res = ftp_size($ftp_conn, $file);
if ($res != -1) {
$total_size = $total_size + $res;
} else {
$total_size = $total_size;
}
}
}
}
ftp_close($ftp_conn);
This doesn't work as it doesn't calculate folder sizes and I don't know how to open the reiterate using this method?
This second script did work but would only work if opened manually, and return 0 if run by the cron job.
class Directory_Calculator {
function calculate_whole_directory($directory)
{
if ($handle = opendir($directory))
{
$size = 0;
$folders = 0;
$files = 0;
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
if(is_dir($directory.$file))
{
$array = $this->calculate_whole_directory($directory.$file.'/');
$size += $array['size'];
$files += $array['files'];
$folders += $array['folders'];
}
else
{
$size += filesize($directory.$file);
$files++;
}
}
}
closedir($handle);
}
$folders++;
return array('size' => $size, 'files' => $files, 'folders' => $folders);
}
}
/* Path to Directory - IMPORTANT: with '/' at the end */
$directory = '../public_html/';
// return an array with: size, total files & folders
$array = $directory_size->size($directory);
$size_of_site = $array['size'];
echo $size_of_site;
Please bare in mind that I am currently testing and none of the MySQLi or PHP scripts are secure yet.
If your server supports MLSD command and you have PHP 7.2 or newer, you can use ftp_mlsd function:
function calculate_whole_directory($ftp_conn, $directory)
{
$files = ftp_mlsd($ftp_conn, $directory) or die("Cannot list $directory");
$result = 0;
foreach ($files as $file)
{
if (($file["type"] == "cdir") || ($file["type"] == "pdir"))
{
$size = 0;
}
else if ($file["type"] == "dir")
{
$size = calculate_whole_directory($ftp_conn, $directory."/".$file["name"]);
}
else
{
$size = intval($file["size"]);
}
$result += $size;
}
return $result;
}
If you do not have PHP 7.2, you can try to implement the MLSD command on your own. For a start, see user comment of the ftp_rawlist command:
https://www.php.net/manual/en/function.ftp-rawlist.php#101071
If you cannot use MLSD, you will particularly have problems telling if an entry is a file or folder. While you can use the ftp_size trick, as you do, calling ftp_size for each entry can take ages.
But if you need to work against one specific FTP server only, you can use ftp_rawlist to retrieve a file listing in a platform-specific format and parse that.
The following code assumes a common *nix format.
function calculate_whole_directory($ftp_conn, $directory)
{
$lines = ftp_rawlist($ftp_conn, $directory) or die("Cannot list $directory");
$result = 0;
foreach ($lines as $line)
{
$tokens = preg_split("/\s+/", $line, 9);
$name = $tokens[8];
if ($tokens[0][0] === 'd')
{
$size = calculate_whole_directory($ftp_conn, "$directory/$name");
}
else
{
$size = intval($tokens[4]);
}
$result += $size;
}
return $result;
}
Based on PHP FTP recursive directory listing.
Regarding cron: I'd guess that the cron does not start your script with a correct working directory, so you calculate a size of a non-existing directory.
Use an absolute path here:
$directory = '../public_html/';
Though you better add some error checking so that you can see yourself what goes wrong.

Warning with, fopen, feof and fgetcsv

I'm in trouble, I am failing to understand why this error is happening.
So when I only run this code,
function getClientProject($cliente)
{
$file = fopen("Projetos.csv","r");
$ArrayCount = 0;
$bool = false;
while(! feof($file))
{
$data = fgetcsv($file);
if($data[0] != '')
{
if(substr_compare($data[0],$cliente, 0, 3) == 0)
{
if($ArrayCount > 0)
{
$total = count($OpenProject);
for($i=0;$i<$total;$i++)
{
if($OpenProject[$i] == $data[0])
$bool = true;
}
if($bool == false)
{
$OpenProject[$ArrayCount] = $data[0];
$ArrayCount++;
}
}else
{
$OpenProject[$ArrayCount] = $data[0];
$ArrayCount++;
}
}
}
}
fclose($file);
return $OpenProject;
}
It works and returns the Array. But when I call the function this way,
include_once 'CRM files/TSread.php';
$arrayC = getClientProject('SLA');
var_dump($arrayC);
No longer works and me these errors,
What am I doing wrong?
path, the file i'm using is "Projeto.php":
and my CRM files folder:
You are opening the file using a relative path. You assume that Projetos.csv is always in the same directory as the TSread.php file. Although, when including it, you seem to be in a higher directory (outside of the CRM files directory), therefor PHP can no longer find your CSV file, since it's trying to open it relative to the upper directory now.
You could pass the full path to your getClientProject method to avoid this. So, you would get something like:
$arrayC = getClientProject('SLA', __DIR__ . '/CRM files/Projectos.csv');
Obviously, you will need to change your function a little to work with this new constructor, so it should like something like this:
function getClientProject($cliente, $csv) {
$file = fopen($csv, "r");
// Followed by the rest of your function

Loading images from a directory into an array with php

So I've killed an entire day trying to do something that would take someone who actually knows how to write php less than 2mins. Frustrating, but I learn by doing and trying to figure things out.
I'll feel like a failure for not getting this, but 8hrs and counting (yeah I know lame) is enough.
Can somebody tell me what's wrong with this equation...
$dir = '../folder';
$images_array = glob($dir.'*.jpg');
$values['options'] = array( '<img src="$images_array"/>');
It's probably obvious, but all I need is for the images in mysite.com/folder to be loaded into the $values['options'] array.
If I simply state the path to a single image in then the image is displayed (obviously because it's not reliant on anything else.)
Thanks.
#hellcode
Sorry about the mess in the 'comment' below your response. Unfortunately I couldn't get this to work? Maybe I need to provide more context.
The images in the folder are going to be used as checkbox items in a form. This was my original code (not working):
add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
if($field->id == 187){
$dir = '../folder';
$images_array = glob($dir.'*.jpg');
$values['options'] = array( '<img src="$images_array"/>');
$values['use_key'] = true;
}
return $values;
}
I added your code like so:
add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
if($field->id == 187){
$dir = '../folder';
$images_array = glob($dir.'*.jpg');
$values['options'] = array();
foreach($images_array as $image) {
$values['options'][] = '<img src="'.$image.'"/>';
}
$values['use_key'] = true;
}
return $values;
}
But it didn't pull the files in unfortunately :(
Try:
$dir = '../folder';
$images_array = glob($dir.'*.jpg');
$values['options'] = array();
foreach($images_array as $image) {
$values['options'][] = '<img src="'.$image.'"/>';
}
Well, one problem may be that the glob() function uses the current directory, which can be anything, unless you use the chdir() function.
One thing that is definitely a problem is that you are using glob()'s return value, $images_array, as a string. Because it is an array that will not work.
Here is something that should work.
// Allowed image formats (also known as a "whitelist")
$allowedFormats = ['jpg', 'jpeg', 'gif', 'png'];
// Array for holding any found images
$foundImages = [];
// Get the real path from the relative path
$path = realpath('../folder');
if ($path === false) {
die('The path does not exist!');
}
// Open a folder handle
$folder = dir($path);
// Read what is in the folder
while (($item = $folder->read()) !== false) {
// .. is the parent folder, . is the current folder
if ($item === '..' or $item === '.') {
continue;
}
// Find the last dot in the filename
// If it was not found then not image file
$lastDot = strrpos($item, '.');
if ($lastDot === false) {
continue;
}
// Get the filetype and make sure it is
// an allowed format
$filetype = substr($item, $lastDot);
if ( ! in_array($filetype, $allowedFormats)) {
continue;
}
// Okay, looks like an image!
$foundImages[] = $item;
}
// Close the folder handle
$folder->close();

PHP Filehandling, code never reached

I've got a piece of code with some error i've been trying to find all day... what this (should) does is: go through all the files in a folder, check if it's a image type, if it is, create a thumbnail in some folder. It does the job BUT: it doesnt continue afterwards, I put a echo command and its never executed. I apprechiate any help! thanks alot.
$bilddateitypen = array('gif','jpg','JPG','GIF');
$importdir = '../sync';
if ($handle = opendir($importdir)) {
while (($file = readdir($handle)) !== false) {
if (!is_dir($importdir."/".$file) && is_file($importdir."/".$file)) {
$ex = explode('.',$file);
if(in_array($ex[count($ex)-1], $bilddateitypen)) {
$file = utf8_decode($file);
$thumbnail = new thumbnail($importdir."/".$file);
$thumbnail->jpeg_quality(93);
$thumbnail->save("../shop/images/product_images/original_images/".$file);
$thumbnail->size_auto(500);
$thumbnail->save("../shop/images/product_images/popup_images/".$file);
$thumbnail->size_auto(200);
$thumbnail->save("../shop/images/product_images/info_images/".$file);
$thumbnail->size_auto(112);
$thumbnail->save("../shop/images/product_images/thumbnail_images/".$file);
} else {
continue;
}
}
}
closedir($handle);
}
echo "Hallo Welt";
It looks like you never update the value of $handle within the while loop, therefore creating an infinite loop.
Try run code only with echo without resizing images. Try var_dump variables and check what is in. Make sure you have error reporting on. If some error occur.
Check image extension this way:
public static function isImage($fileName)
{
$type = exif_imagetype($fileName);
if (!(($type == IMAGETYPE_JPEG) || ($type == IMAGETYPE_GIF) || ($type == IMAGETYPE_PNG)))
return false;
return true;
}

Categories