PHP/(bad) exif data/warnings, what to do?

后端 未结 2 1026
星月不相逢
星月不相逢 2020-12-06 18:55

I\'m writing a small script that gathers a couple exif values from images... namely the creation date, make and model.

I\'m noticing (particularly with image mailed

相关标签:
2条回答
  • 2020-12-06 18:56

    You can use the @ operator to hide the warning without using display_errors, i.e.

    $exif = @exif_read_data(..);
    

    That's better than setting display_errors because it silences warnings/errors on the exif read function only, and does not hide other possible errors in your code.

    0 讨论(0)
  • 2020-12-06 19:00

    Despite this is on old topic, it came for me out of nowhere with new php 7.2: Bug #75785 Many errors from exif_read_data

    I agree with @maraspin, as any error is for a reason and not dealing with it means having poor performance (time, features).

    MY GOAL: to get 'DateTimeOriginal' of uploadable image (and not just creation_date of tmp file - DateTime).

    1. Normal using of exif_read_data:

    $exif = exif_read_data(tmp/phpTBAlvX); or
    $exif = exif_read_data($file->tempName, 'ANY_TAG'); or
    $exif = exif_read_data($file->tempName, 'IFD0'); or
    $exif = exif_read_data($file->tempName, 'EXIF');
    

    PHP Warning – yii\base\ErrorException exif_read_data(tmp/phpTBAlvX): Process tag(x010D=DocumentNam): Illegal components(0)

    2. Using the @ operator to hide the warning:

    $exif = @exif_read_data(tmp/phpTBAlvX);
    

    RESULT: $exif as array with 20 arguments, but no 'DateTimeOriginal' in it

        Array (
        [FileName] => phphT9mZy
        [FileDateTime] => 1529171254
        ...
        [SectionsFound] => ANY_TAG, IFD0, EXIF
        [COMPUTED] => Array
            (
                [html] => width="3968" height="2976"
                [Height] => 2976
                [Width] => 3968
                ...
            )
        [ImageWidth] => 3968
        [ImageLength] => 2976
        [BitsPerSample] => Array()
        [ImageDescription] => cof
        [Make] => HUAWEI
        ...
        [DateTime] => 2018:06:14 12:00:38
        [YCbCrPositioning] => 1
    )
    

    3. Ended with solution:

    $img = new \Imagick(tmp/phpTBAlvX);
    $allProp = $img->getImageProperties();
    $exifProp = $img->getImageProperties("exif:*");
    

    RESULT: $allProp as array with 70 arguments with 'DateTimeOriginal'

    Array (
        [date:create] => 2018-06-16T21:15:24+03:00
        [date:modify] => 2018-06-16T21:15:24+03:00
        [exif:ApertureValue] => 227/100
        [exif:BitsPerSample] => 8, 8, 8
        ...
        [exif:DateTimeOriginal] => 2018:06:14 12:00:38
        [jpeg:colorspace] => 2
        [jpeg:sampling-factor] => 2x2,1x1,1x1
    )
    

    RESULT: $exifProp as array with 66 arguments with 'DateTimeOriginal'

    Array (
        [exif:ApertureValue] => 227/100
        [exif:BitsPerSample] => 8, 8, 8
        ...
        [exif:DateTimeOriginal] => 2018:06:14 12:00:38
    )
    

    MY DECISION:

    1. never use @ for suppressing any warning or code
    2. use The Imagick class for getting any tag of an image
    0 讨论(0)
提交回复
热议问题