0

I created a code to upload multiple images to a folder with different extensions e.g. gif, jpg, bmp, etc...

I would like to convert all images in the folder to jpg. I trie instances with rename, but the code that I found gave me double file extensions e.g. hello.gif.jpg or bye.jpg.jpg

The latest code I tried to do the above process is using the following:

<?php
$directory = "img/";
$images = glob($directory . "*.jpg"); 
$a = 0;
foreach($images as $image)
{
$a++;
rename($image, "test/".$a.".jpg"); 
}
?>

I didn't get any errors. Rather than convert images from various formats to jpg I would like to just change the extension.

Panny Monium
  • 301
  • 2
  • 4
  • 20
  • 3
    unix-type rename operations do NOT work like dos-style `ren *.gif *.bmp`. You'll have to decompose the original filenames, stripping off the original extension, and then adding on the `.jpg` suffix. And note that this doesn't change their actual format, they'll just be a gif/bmp/whatever LYING about being a jpg. – Marc B Oct 08 '13 at 15:38
  • @MarcB I am aware and understand but this is what I have in mind doing for the time being. – Panny Monium Oct 08 '13 at 15:41

3 Answers3

2

I would like to convert all images in the folder to jpg

The script in the question is not doing that, it's simply renaming files; a gif named foo.jpg is still a gif.

A browser may ignore the headers and display the misnamed image file correctly (to a browser the filename/url is unimportant - the headers are what determine the format, however the webserver will send the content-type header based on the filename) - It may however simply treat the image as a broken jpeg and display nothing.

Converting files is more than renaming them

You can use tools such as ImageMagik to correctly convert image files from one format to another.

A script to use ImageMagik (cheating via cli since it's simpler to use) would be for example:

<?php
$directory = "img/";
chdir($directory);
$images = glob($directory . "*"); 
foreach($images as $image) {
    if (substr($image, -4) === '.jpg') {
        continue;
    }
    $newname = preg_replace('@\..*$@', '.jpg', $image);
    `convert $image $newname`;
}

Use the right tool for the job

Note that the above PHP script may work but there are more appropriate tools that can be used. For example mogrify is another cli provided by ImageMagik which can be used to perform batch operations. Converting all png files to jpeg in a single folder is this simple:

$ cd imgs/are/here/
$ mogrify -format jpg *.png
AD7six
  • 63,116
  • 12
  • 91
  • 123
0

You're getting double file extensions because the name of a file is, for example, hello.gif. You're then appending .jpg on the end which makes it hello.gif.jpg.

You must first trim off the extension from the filename, and then save it with the new name. Like so:

<?php

$directory = "img/";
$images = glob($directory . "*.jpg"); 
$a = 0;

foreach($images as $image) {
    $a++;
    $trimmed = substr($image, 0, strpos($name, '.'));
    rename($image, "test/" . $trimmed . ".jpg"); 
}

substr($image, 0, strpos($name, '.')) will ensure you only get the file name, nor the extension. It does this by removing the . and any characters after it.

John Dorean
  • 3,744
  • 9
  • 51
  • 82
  • `pathinfo()` does the full file/path deconstruction for you, without having to do manual string operations. – Marc B Oct 08 '13 at 15:43
  • I'd recommend using pathinfo to determine the file name/extension (as someone could upload a file named `'hello.image.jpeg'`) – giorgio Oct 08 '13 at 15:43
  • I didn't realize `pathinfo()` allowed you to get the filename without the extension, that's handy! I'll leave it for someone else to answer though :) – John Dorean Oct 08 '13 at 15:47
0

What you're doing is equivalent to renaming a document file to an excel file. They're not meant to be compatible even though they have similar purposes (displaying an image).

You'd be better off looping through the images (which is what you've already done), determine the type of image it is and based off that, change the format.

An example if exif_imagetpye() returns IMAGETYPE_PNG.

When that happens, take a look at this solution

If you want to purely rename the file and not care about the type of document:

foreach ( $images as $image ) {
   $file_info = pathinfo($image);
   rename($image, $file_info['filename'].".jpg");
}
Community
  • 1
  • 1
Duniyadnd
  • 4,013
  • 1
  • 22
  • 29