Replace .dds to .png? - php

I am looking for an way to replace .dds to .png with php.
The reason is this every images is automaticly inserted into the database with the extension .dds the .dds is an format that the game uses to render its images so i cant replace this.
So what i do is fetch the icons from the database and then the file name must be changed to .png before i insert it into an other database.
I have tried it with preg_replace but i need some help on how to set it up.

$str = str_replace('.dds', '.png', $mystring);

I don't think replacing extension from DDS to PNG is enought. I think you should rather convert from DDS to PNG:
<?php
$url = 'http://server.com/image.dds';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/image_convert.php?url=' . $url . '&format=png'));
if (#$data->success !== 1)
{
die('Failed');
}
$image = file_get_contents($data->file);
file_put_contents('rendered_page.png', $image);

Related

How can Fopen read png files in php

I was working on something and I needed the data out of a png file and I can only use PHP. Don't ask why just give me the answer lol
Lets suppose below is your image.
You have to define its url in a variable using the below line
$image = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
after that you can use file_get_content function to get data/content of image.
$image_content = file_get_contents($image);
also, if you want to convert it into base64 string, you can use base64_encode function.
$image_base64Data = base64_encode($image_content);
Whole Code will be ......
<?php
$image = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
$image_content = file_get_contents($image);
$image_base64Data = base64_encode($image_content);
?>

Duplicating an image from a file path, changing name and saving it to a different table in laravel

I have a file path on a table, what I am trying to do is create the image from the file path and then save the 'new' file with a different name.
Because I have only the file path, I do not now how to create the image object so that I can then getClientOriginalExtension(); and save that to the database. I have tried the following:
$img = $var->image_path;
$file = file_get_contents($img);
$filename = time() . '.' . $file->getExtension();
Image::make($file)->resize(300, 300)->save( public_path('/test' . $filename ) );
However the script errors: Call to a member function getExtension() on string what would be the right way with the file path, create the object, change the name of the file, ensure the right extension is set (maybe outside scope of this q) and then save the newly created image to a different folder and save the newly created image path to the database.
I hope that makes sense.
Update: should I use file_put_contents() instead?
Achieved what I wanted using the copy() and iterating over each image and adjusting the image relatively.
$title = $var->name;
$string = str_replace(' ', '-', $title); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
copy($img, public_path('/test' . $string . time()));
http://php.net/manual/en/function.copy.php

How to processing an image downloaded from AWS S3 with Laravel 5?

I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that
Storage::disk('s3')->get(imageUrlonS3);
retrieve the image in binary and is giving me errors. This is my code:
function createSlices($imagePath) {
//create transform driver object
$im = imagecreatefromjpeg($imagePath);
$sizeArray = getimagesize($imagePath);
//Set the Image dimensions
$imageWidth = $sizeArray[0];
$imageHeight = $sizeArray[1];
//See how many zoom levels are required for the width and height
$widthLog = ceil(log($imageWidth/256,2));
$heightLog = ceil(log($imageHeight/256,2));
//more code here to slice the image
.
.
.
.
}
// ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg
$content = Storage::disk('s3')->get(imageUrlonS3);
createSlices($content);
What am I missing here ?
Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself.
If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have.
Here's the relevant documentation:
createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php
getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php
Resulting code might look something like this:
function createSlices($imageData) {
$im = imagecreatefromstring($imageData);
$sizeArray = getimagesizefromstring($imageData);
//Everything else can probably be the same
.
.
.
.
}
$contents = Storage::disk('s3')->get($imageUrlOnS3);
createSlices($contents);
Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.

base64 decode mixed results

I have the following code in PHP, and it works for the most, fine. I am sending a image from a mobile device to this script, which decodes it into a img file and creates a file out of it on the server. I am 99.9% sure every time its a base64 encoded.
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: image/jpeg');
$data = ($_POST['imageData']);
define('UPLOAD_DIR', 'images/');
$img = str_replace('data:image/jpeg;base64,', '', $data);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.jpg';
file_put_contents($file, $data);
echo ('{"imgUrl" : "' . $file . '"}');
?>
This then returns the image URL back to be added to a database.
The problem is, most of the time it does decode into a .jpg file, and other times into a txt file. I cannot see why it does it, as its a little random. But I have noticed that sometimes it will come as a $_POST, and other times, $_POST is Null. So I looked at using:
$data = json_decode(file_get_contents('php://input'));
But again, it seems inconsistant. But I put a logic statement such as:
$data = ($_POST['imageData']);
if($data == NULL) {
$data = json_decode(file_get_contents('php://input'));
}
Is there any reason I should be aware of why the code works, and sometimes does not work ?
I know this question is old, but is one of the first appearance by looking at this topic. So everyone looking at this question can find a link to a proper answer right away.
You should check this PHP - get base64 img string decode and save as jpg (resulting empty image )
Also check the conditions you're using, because
if ($data === NULL)
it may be different for
if ($data == NULL)
Also, you're saving the base64 string incorrectly to an image file.
Check that link and let me know how if it helped.

PHP/regex : Script to create filenames with dashes instead of spaces

I want to amend a PHP script I'm using in wordPress (Auto Featured Image plugin).
The problem is that this script creates filenames for thumbnails based on the URLs of the image.
That sounds great until you get a filename with spaces and the thumbnail is something like this%20Thumbnail.jpg and when the browser goes to http://www.whatever.com/this%20Thumbnail.jpg it converts the %20 to a space and there is no filename on the server by that name (with spaces).
To fix this, I think I need to change the following line in such a way that $imageURL is filtered to convert %20 to spaces. Sound right?
Here is the code. Perhaps you can tell me if I'm barking up the wrong tree.
Thank you!
<?php
static function create_post_attachment_from_url($imageUrl = null)
{
if(is_null($imageUrl)) return null;
// get file name
$filename = substr($imageUrl, (strrpos($imageUrl, '/'))+1);
if (!(($uploads = wp_upload_dir(current_time('mysql')) ) && false === $uploads['error'])) {
return null;
}
// Generate unique file name
$filename = wp_unique_filename( $uploads['path'], $filename );
?>
Edited to a more appropriate and complete answer:
static function create_post_attachment_from_url($imageUrl = null)
{
if(is_null($imageUrl)) return null;
// get the original filename from the URL
$filename = substr($imageUrl, (strrpos($imageUrl, '/'))+1);
// this bit is not relevant to the question, but we'll leave it in
if (!(($uploads = wp_upload_dir(current_time('mysql')) ) && false === $uploads['error'])) {
return null;
}
// Sanitize the filename we extracted from the URL
// Replace any %-escaped character with a dash
$filename = preg_replace('/%[a-fA-F0-9]{2}/', '-', $filename);
// Let Wordpress further modify the filename if it may clash with
// an existing one in the same directory
$filename = wp_unique_filename( $uploads['path'], $filename );
// ...
}
You better to replace the spaces in image name with underscores or hypens using regexp.
$string = "Google%20%20%20Search%20Amit%20Singhal"
preg_replace('/%20+/g', ' ', $string);
This regex will replace multiple spaces (%20) with a single space(' ').

Categories