Re: Next question about data output...<smile>

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If I sort of combine all the relevant code - since it's currently a page instantiating a classe called template, which then calls a function from another sort of wrapperr class, it's effectively the following - will add comments in to show where it might be changing context slightly - and, below all of this, will include a dropbox link to 3 actual files themselves:

//start code - in template_image.php webpage
if (isset($_GET["id"])) {
//following line includes image_manipulation class code
require("../imageManipulation.php");
//and following line includes template class code
require("../template.php");
$templ = new template  ($_GET["id"]);
$templ->renderTemplate();

//next piece of code, inside template class
//following comes out of renderTemplate function

if ($this->iID>0) { //$this->iID gets set during __construct
//this instantiates a reference to other wrapper class
$im = new image_manipulation;
$im->mysqliObj = $this->mysqliObj; //$this->mysqliObj is an instance of mysqli
$imgOut = $im->dbGetImage("tbl_budget_templates", "b_image", $this->iID);

//here's the code inside the dbGetImage function inside the image_manipulation class

if (!is_null($this->mysqliObj)) {
$sSQL = sprintf("select %s from %s where id = %s;", $sField, $sTableName, $iID);
$stmnt = $this->mysqliObj->prepare($sSQL);
echo "just before execution: " . memory_get_usage() . "<br />\n";
$stmnt->execute();
echo "after execution: " . memory_get_usage() . "<br />\n";
$stmnt->bind_result($bOut);
$stmnt->fetch();
$stmnt->close();
echo "after execute and fetch: " . memory_get_usage() . "<br />\n";
//here's where the memory in use had jumped to over 4Mb

$imgOut = imagecreatefromstring($bOut);
unset($bOut);
return $imgOut;
imagedestroy($imgOut);
} else {
return NULL;
}

//back to code inside template class
echo "after retrieving img resource to template: " . memory_get_usage() . "<br />\n";
if (!(is_null($imgOut))) {
imagepng($imgOut, $this->sTempPath . "temp.png");
$sSize = strval(filesize($this->sTempPath . "temp.png"));
unlink($this->sTempPath . "temp.png");
echo "after creating and deleting image file: " . memory_get_usage() . "<br />\n";
//commented out following rendering lines while testing memory usage

//header('HTTP/1.1 200 OK', True, 200);
//header('Content-Type: image/png');
//header('Content-Disposition: ' . $sDisposition . '; filename="template.png"');
//header('Content-Transfer-Encoding: binary');
//header('Content-Length: ' . $sSize);

//imagepng($imgOut);
return memory_get_usage();
imagedestroy($imgOut);
} else {
echo NULL;
}//end of checking if $imgOut != NULL
}//end of checking if iID>0

//back to code in template_image.php
}//end of checking for querystring ID

//end of code

Here's the zip file with those three files in it, and, FWIW, image_template is from a subfolder, called admin, and in the root folder of the site, along with the two other class files, among various other pages, there's another subfolder, called tempimg, to store temporary image output files in - it has read/write permissions, but, no execute - just on principle:
https://dl.dropboxusercontent.com/u/13327195/tempCode.zip

And, not totally relevant, but, what used before to just render output images was following - was the renderTemplate function mentioned above that did it all itself - and it seemed to work alright, but, hadn't actually, as of yet, stored any of the really large images in database on production server at that stage:

public function renderTemplate($sDisposition="inline") {
if ($this->iID>0) {
$sSQL = sprintf("select b_image from tbl_budget_templates where id = %s and b_image IS NOT NULL;", $this->iID);
$res = $this->mysqliObj->query($sSQL);
if ($res->num_rows>0) {
$row = $res->fetch_assoc();
header('HTTP/1.1 200 OK', True, 200);
header('Content-Type: image/png');
header('Content-Disposition: ' . $sDisposition . '; filename="template.png"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($row["b_image"]));

imagepng(imagecreatefromstring($row["b_image"]));
} else {
echo "no record retrieved - " . $sSQL;
}
} else {
echo NULL;
}//end of checking if iID>0
}//end of renderTemplate function

Thanks

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'

----- Original Message ----- From: "Sascha Meyer" <harlequin2@xxxxxx>
To: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
Sent: Friday, August 16, 2013 1:39 PM
Subject: Aw: Re:  Next question about data output...<smile>


Jacob,

could you post the part of your code where the error occurs? Perhaps we can optimize some of the code to reduce the amount of memory used by the script.

Cheers,

Sascha

Gesendet: Freitag, 16. August 2013 um 12:15 Uhr
Von: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
An: "Sascha Meyer" <harlequin2@xxxxxx>
Cc: php-windows@xxxxxxxxxxxxx
Betreff: Re:  Next question about data output...<smile>

OK, seems what's causing issue is imagecreatefromstring(), since, directly
after retrieving longblob data from database, memory usage is:
3402136 = 3.*Mb

and, then immediately after that calling:
$imgOut = imagecreatefromstring($bOut);

and, return that $imgOut variable to calling function, before do anything
else, memory usage:
44063856 - 44Mb+

And, this is on my windows development machine, so seems issue is with
operations, and not machine specific, although not sure why it's working on
this machine, and not on production server.

Either way, think will, for now, switch image output rendering back to
simpler method that worked before, instead of using actual mysqli_statement,
and see if works as such - might also put the memory usage 'logging' in
there, to actually compare them, or something.

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'

----- Original Message ----- From: "Sascha Meyer" <harlequin2@xxxxxx>
To: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
Sent: Friday, August 16, 2013 8:57 AM
Subject: Aw:  Next question about data output...<smile>


> Good morning Jacob,
>
> could you check if memory has been eaten up before the mysqli_statement > is
> executed? memory_get_usage() [1] could help in that case. Are you using
> different PHP versions on your local dev machine and on the production
> server?
>
> Best regards,
>
> Sascha
>
> [1] http://php.net/manual/en/function.memory-get-usage.php
>
>> Gesendet: Freitag, 16. August 2013 um 08:42 Uhr
>> Von: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
>> An: php-windows@xxxxxxxxxxxxx
>> Betreff:  Next question about data output...<smile>
>>
>> On my development, windows machine, the memory_limit iniValue is set >> to >> 128M, and on the production server - think linux box, it's set to >> 1280M, >> but, the bit of script to render an image from database as image >> resource >> to browser, works fine on my machine, but, on server, the following >> error
>> occurs:
>> PHP Fatal error: Allowed memory size of 1342177280 bytes exhausted >> (tried
>> to allocate 4294967296
>>
>> According to server log, it seems to be happening on the line where >> the >> mysqli_statement tries to execute the query to retrieve a longblob >> value
>> of 2.1Mb, but anyway.
>>
>> I am making sure try to destroy any image resources, etc., am closing
>> statement object after executing it, etc. - and there aren't really >> any
>> recursive loops being executed, but, just a page instantiating an
>> instance of a wrapper class, which then refers to the >> imageManipulation
>> object, to then pull the images data out of the DB - thoughts?
>>
>> TIA
>>
>> Jacob Kruger
>> Blind Biker
>> Skype: BlindZA
>> '...fate had broken his body, but not his spirit...'
>>
>


--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux