Jump to content

Rename an image


AlexL
 Share

Recommended Posts

Hi guys

Firstly - I LOVE PROCESSWIRE! I've had a little bit to do with modx and kentico and boy do I love working in PW.  Most problems I've been able to solve on my own due to the super helpful responses already in the forums - but I've hit a hitch.

The core of the problem is the image resizer is not working for me.  I believe that the issue relates to a file that has a .png extension but when I look at exif it is acutally a jpeg.  So I want to rename it.  When I load the image it is from an external URL

//Bootstrap ProcessWire
include("/[PATH]/index.php");

$page = new Page(); 
[FIELD SETTING FOR NEW PAGE]

$page->save();
		
$page->Image = $row['imageUrl']; //$row is from previous MYSQL select
$page->save();

so far so good - except now I want to pass that file through exif_imagetype to check what the actual image type is and then rewrite the file with the correct extension.

Something like

if (exif_imagetype($image->url) = IMAGETYPE_JPEG) {
    $image->ext = 'jpg';
}

etc...

However I'm getting 'Can't use function return value in write context' from the CLI for the exif command.

Sorry it's so long winded I guess my questions are

1. Is it possible to pass the file to exif? What am I doing wrong above?

2. How do I programatically rewrite the extension?

3. Am I doing this completely wrong and should I be writing a hook?

Thanks loads!!!

Alex

Link to comment
Share on other sites

Welcome Alex! Ex-MODX-er here. Yes, PW is really awesome :)

Our resident image (exif) etc expert is a guy called Horst. He (and others) will give you a better answer than me. Maybe the following threads would be helpful?

http://processwire.com/talk/topic/238-creating-pages-using-api/

http://processwire.com/talk/topic/3219-images-manager-alpha/?p=33485

http://processwire.com/talk/topic/3398-working-with-processwire-getting-exif-data-from-images/

Link to comment
Share on other sites

Hi Alex, welcome to the forum.

I believe that the issue relates to a file that has a .png extension but when I look at exif it is acutally a jpeg. So I want to rename it. When I load the image it is from an external URL

$page = new Page(); [FIELD SETTING FOR NEW PAGE]$page->save();		
$page->Image = $row['imageUrl']; //$row is from previous MYSQL select$page->save();


To be save: your $page->Image is a real ImageInputfield that is assigned to a template?
With [FIELD SETTINGS FOR NEW PAGE] you also have set the right template (that one that contains an ImageInputField named Image) ??


Assuming the both questions answered with yes, you can pass the images filename to the exif_imagetype function:

<?php
$map = array( IMAGETYPE_JPEG=>'jpg', IMAGETYPE_PNG=>'png', IMAGETYPE_GIF=>'gif' );
$url = 'http://nogajski.de/_grafik/0815.png';  // a jpeg with wrong extension!

$p = new Page();                               // create page and assign image from url 
  ....
$p->save();
$p->Image = $url;
$p->save();
$img = $p->Image->first();                     // get your ImageObject, * read below ...
$ext_im = $map[exif_imagetype($img->filename)];// compare imagetype against file extension
$ext_fn = strtolower($img->ext);
if($ext_im != $ext_fn) {                       // if not match, rename it	
    $newFilename = str_replace('.'.$img->ext, '.'.$ext_im, $img->name);	
    $img->rename($newFilename);	
    $p->save();
}

* how to get the imageObject depends a bit on your settings for the ImageField under Setup->Fields-> (your Image field) -> Details -> Maximum files allowed.
If you have set it to 1 or if you allow multiple. (not really sure) but I think this could work for both situations

EDIT: -> http://cheatsheet.processwire.com/  the FILES -> properties and ->methods()  :-)

Edited by horst
  • Like 3
Link to comment
Share on other sites

OK - that looks exactly like what I'm after!!  

After reading your code I've just noticed the $file->rename and found it in the cheatsheet as well - feeling a little silly  :unsure:  - thanks heaps!!!  :cool:

Can't wait to try it out - love learning all these new things :D

Alex

  • Like 1
Link to comment
Share on other sites

 After reading your code I've just noticed the $file->rename and found it in the cheatsheet as well - feeling a little silly  :unsure:

There is so much to explore and learn with PW, - so, only overlook a single method isn't that bad. ;)

I have to go to CheatSheet too when writing the code example. First, before looking to the CheatSheet, I have tried with $img->path instead of your $img->url but the right property is $img->filename. It takes time to learn all the usefull stuff. The CheatSheet is my best friend besides the great people here. :-)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...