1

I have a resource object (image) that has been created from imagecreatetruecolor after lots of process.

$image = imagecreatetruecolor($dst_width, $dst_height);

Last step is adding iptc tags to the image.

For adding iptc tags php has native function which is called iptcembed

iptcembed ( string $iptcdata , string $jpeg_file_name [, int $spool ] );

Problem is : I'm storing image as resource object. But iptcembed needs image as file path string $jpeg_file_name.

For every image I should save image and load it from iptcembed for iptc tags.

This is big performance issue. Also dirty codes.

I suppose that php wrappers could be solution for this problem but I learned that they are not paths. They are only references. Following code did not worked for me.

    $data = null;
    ob_start();
    imagejpeg($this->image['src_image'], null, $compression);
    $data = ob_get_contents();
    ob_end_clean();

    $img = fopen("php://temp", 'wb+');
    fwrite($img, $data);
    fclose($img);

    $content = iptcembed('', "php://temp");

Question is : Are there any trick that I can reach to this imagepath from memory / of better way to accomplish that?

Deniz Porsuk
  • 492
  • 1
  • 6
  • 20

1 Answers1

0

same problem here :)

i have high-resolution "master" jpeg image streams stored in a database. i must serve download requests with random resize, crop and compress parameters. the last step would be adding some metadata to the created temporary image.

i'm using PHP 5.6 (Windows 7) and 7.2 (Ubuntu 18). my fastest solution so far is a small, dedicated ramdisk. on Ubuntu, i simply created and mounted a tmpfs filesystem:

$ sudo bash
# mkdir -p /media/ramdisk
# chmod 1777 /media/ramdisk
# mount -t tmpfs -o size=256M tmpfs /media/ramdisk
# grep /media/ramdisk /etc/mtab | tee -a /etc/fstab
tmpfs /media/ramdisk tmpfs rw,relatime,size=262144k 0 0
# ^D
$ df -m /media/ramdisk
Filesystem     1M-blocks  Used Available Use% Mounted on
tmpfs                256     0       256   0% /media/ramdisk
$

on Windows, i installed the free RAM Disk application from SoftPerfect (available here: https://www.softperfect.com/products/ramdisk/).

C:\>dir k:
 Volume in drive K is Ram Disk
 Volume Serial Number is 5566-7788

 Directory of K:\

2018-11-14  19:18    <DIR>          cache
2018-11-14  19:18    <DIR>          temp
               0 File(s)              0 bytes
               2 Dir(s)     117 518 336 bytes free

i performed a small benchmark recently (on my Windows PC) with a sample jpeg image. the image was 1.83 MB of size and 1520x1200 pixels of resolution; i wrote it out and read it back 1000 times to/from my HDD and my RAM disk.

on an idle system (not that anyone could tell whether his windows is idle or not :) ), i measured about 8 seconds (7.3s - 9.1s) response time on each storage type.

then i started copying some files across my HDDs. the ram disk behaved the same as before, but - not surprisingly - the HDDs' response started fluctuating between 8 and 40 seconds.

so, i came up with the following pseudo code snippet:

$raw = ...; // obtain untagged jpeg stream //
$temp = '/path/to/file/on/ram/disk';
file_put_contents($temp, $raw);
$meta = ...; // set the metadata as needed //
$raw = iptcembed($meta, $temp); // reload tagged image stream //
unlink($temp); // don't forget to clean up //

which is, of course, almost the same as yours.

ThatsMe
  • 386
  • 2
  • 9
  • i guess the main reason of an on-the-fly function not being available is the fact that such a function would *immediately* reveal the GD library's own comment in the jpeg stream: "CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 100". – ThatsMe Nov 16 '18 at 14:34