#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Altering data in php array


    I am having a problem replacing the content of a php array which seems to be beyond my limited knowledge of coding, and wonder if someone can point me in the right direction.

    - My first task was to resize all the images in a folder - I have a script that works for doing this.
    - Next I wanted to extract the metadata from the image before resizing, then add it back to the resized image - I modified the script to do this with only one issue. The metadata for the image size is retained and is added to the resized image.

    When the Metadata is extracted from the original image it is held as an array. If I dump the array I can see the information I need to change which shows as

    Code:
    [40962]=> array(9) { ["Tag Number"]=> int(40962) ["Tag Name"]=> string(17) "Pixel X Dimension" ["Tag Description"]=> string(0) "" ["Data Type"]=> int(4) ["Type"]=> string(7) "Numeric" ["Units"]=> string(6) "pixels" ["Data"]=> array(1) { [0]=> int(2000) } ["Text Value"]=> string(11) "2000 pixels" ["Decoded"]=> bool(true) }
    The full array can be seen here

    To replace the required data (in the above example it is the value '2000') I used this:

    Code:
    $exif_data[0][40962]['Data'][0] = $new_width;
    $exif_data[0][40962]['Text Value'] = $new_width . ' pixels';
    The variable $new_width having been set previously.

    The problem is that this code ADDS a new value to the array rather than changing the existing content.

    I tried a further test to see if the code was correct and tried
    Code:
    $test = $exif_data[0][40962]['Data'][0];
    $test2 =  $exif_data[0][40962]['Text Value']
    
    var_dump $test;
    var_dump $test2;
    Both returned NULL

    So I am obviously missing something from the code although looking at the full array printed at the above link I thought it was correct.

    Can anyone suggest what I am missing ??

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,343
    Rep Power
    594
    Please enclose your code in [ PHP ] tags. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    You may not have provided enough information but it looks to me like your statement should be:
    PHP Code:
    $exif_data[40962]['Data'][0] = $new_width;
    $exif_data[40962]['Text Value'] = $new_width ' pixels'
    It is not clear from what you posted why there is that first '[0]'.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by gw1500se
    Please enclose your code in [ PHP ] tags. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.
    Apologies - missed that.

    Originally Posted by gw1500se
    You may not have provided enough information but it looks to me like your statement should be:
    PHP Code:
    $exif_data[40962]['Data'][0] = $new_width;
    $exif_data[40962]['Text Value'] = $new_width ' pixels'
    It is not clear from what you posted why there is that first '[0]'.
    I used the first '[0]' because it was suggested by someone that this was required based on the content of the array which is displayed using the link I posted in my OP (ie http://uko.com/testexif2/phptest.php ). NOTE - I post the link to see the content as pasting it here would take up sooooo much space.

    However, using that sequence of code inserts new values in the array rather than replacing what I need to replace - so if anything it appears to me that there should be some further content in the code to locate the correct point - however with my limited coding knowledge I can't figure out what.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,343
    Rep Power
    594
    It is adding the value because one or more of the preceding elements does not exist. If the dump of the array is really what you posted then the '[0]' is wrong. You don't need to post the entire script, just post the relevant section(s). The best way to see exactly how the array is formed it to use 'print_r'.
    PHP Code:
    echo "<pre>";
    print_r($exif_data);
    echo 
    "</pre>"
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by gw1500se
    It is adding the value because one or more of the preceding elements does not exist. If the dump of the array is really what you posted then the '[0]' is wrong. You don't need to post the entire script, just post the relevant section(s). The best way to see exactly how the array is formed it to use 'print_r'.
    PHP Code:
    echo "<pre>";
    print_r($exif_data);
    echo 
    "</pre>"
    To clarify.

    What I posted is an extract from the results achieved using 'print_r' showing the data I need to change. The link I posted is a simple script displaying the full results of the function 'print_r' - ie the array content .

    You will see that the full results are just one long stream of data with no format making it difficult for me to identify what exactly needs to be entered as code content. That is my problem.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,343
    Rep Power
    594
    That is why you use the <pre>tags. The data is formatted so it is easier to discern.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by gw1500se
    That is why you use the <pre>tags. The data is formatted so it is easier to discern.
    The script includes the <pre> tags exactly as you posted - but the resultant page is still displaying a continuous line of text to display the data.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,343
    Rep Power
    594
    I don't think so. Post the code.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by gw1500se
    I don't think so. Post the code.
    My Apologies - checked the setup again and was looking at the wrong file.
    The correct file is located at - http://uko.com/testexif2/toolkit/phptest.php

    Here is an extract that shows the beginning of the print followed by the section '[40962]' I need to modify.

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    Tags Name] => TIFF
                
    [Tiff Offset] => 12
                
    [271] => Array
                    (
                        [
    Tag Number] => 271
                        
    [Tag Name] => Make (Manufacturer)
                        [
    Tag Description] => 
                        [
    Data Type] => 2
                        
    [Type] => String
                        
    [Units] => 
                        [
    Data] => Array
                            (
                                [
    0] => Panasonic
                            
    )

                        [
    Text Value] => Panasonic
                        
    [Decoded] => 1
                    
    )

                [
    272] => Array
                    (
                        [
    Tag Number] => 272
                        
    [Tag Name] => Model
                        
    [Tag Description] => 
                        [
    Data Type] => 2
                        
    [Type] => String
                        
    [Units] => 
                        [
    Data] => Array
                            (
                                [
    0] => DMC-FZ150
                            
    )

                        [
    Text Value] => DMC-FZ150
                        
    [Decoded] => 1

    .
    .
    .
    .
    .
    CODE IN THIS SECTION REMOVED FOR CLARITY
    .
    .
    .
    .
    .
    .
                     [
    40962] => Array
                                            (
                                                [
    Tag Number] => 40962
                                                
    [Tag Name] => Pixel X Dimension
                                                
    [Tag Description] => 
                                                [
    Data Type] => 4
                                                
    [Type] => Numeric
                                                
    [Units] => pixels
                                                
    [Data] => Array
                                                    (
                                                        [
    0] => 2000
                                                    
    )

                                                [
    Text Value] => 2000 pixels
                                                
    [Decoded] => 1
                                            
    )

                                        [
    40963] => Array
                                            (
                                                [
    Tag Number] => 40963
                                                
    [Tag Name] => Pixel Y Dimension
                                                
    [Tag Description] => 
                                                [
    Data Type] => 4
                                                
    [Type] => Numeric
                                                
    [Units] => pixels
                                                
    [Data] => Array
                                                    (
                                                        [
    0] => 1500
                                                    
    )

                                                [
    Text Value] => 1500 pixels
                                                
    [Decoded] => 1
                                            

    Would you be able to tell from this where I am going wrong in my code?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,343
    Rep Power
    594
    Post the code where you build that array.

    After thought: Instead, print_r the array after you try to update it.
    Last edited by gw1500se; May 19th, 2013 at 08:34 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by gw1500se
    Post the code where you build that array.

    After thought: Instead, print_r the array after you try to update it.
    I've put the update code in the script.
    PHP Code:
    $exif_data[0][40962]['Data'][0] = $new_width;
    $exif_data[0][40962]['Text Value'] = $new_width ' pixels'
    I added a line (echo) 'Now lets print the Array after update' as a divider and then printed the array a second time.

    If you search the results page you'll see it has added

    PHP Code:
    [40962] => Array
                    (
                        [
    Data] => Array
                            (
                                [
    0] => 1000
                            
    )

                        [
    Text Value] => 1000 pixels 
    as new data about half way through the updated detail.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,343
    Rep Power
    594
    Here is what I see it should be:
    PHP Code:
     $exif_data[0][34665]['Data'][0][40962]['Data'][0] = $new_width;
    $exif_data[0][34665]['Data'][0][40962]['Text Value'] = $new_width ' pixels'
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by gw1500se
    Here is what I see it should be:
    PHP Code:
     $exif_data[0][34665]['Data'][0][40962]['Data'][0] = $new_width;
    $exif_data[0][34665]['Data'][0][40962]['Text Value'] = $new_width ' pixels'
    GW - That worked perfectly.

    For some reason I couldn't see it when I was trying to figure it out but I can see how you arrived at it now it's in front of me.


    Out of over 400 views on various forums you were the first to respond (in any sort of meaningful way), so your help is very much appreciated.

IMN logo majestic logo threadwatch logo seochat tools logo