How to set PIL saved image "content type" as 'image/jpeg'? - firebase

I'm reading an image with PIL, resizing it, and saving again as jpeg, as such:
img = np.array(Image.open(filename))
# I'm using TensorFlow for resizing, but it doesn't matter
resized_img = tf.keras.preprocessing.image.smart_resize(img, (335, 335))
resized_img = Image.fromarray(resized_img.astype('uint8')).convert('RGB')
resized_img.save(f'thumb/{filename}_thumb', format='jpeg', quality=70)
When I upload it to Firebase Storage and I try to access it with the direct link, the file gets downloaded instead of displaying the image, which is something I don't want.
It can be viewed locally normally. But the Type is shown as application/octet-stream at the console, and it can also be displayed normally with <img> tags.
EDIT: I could solve the content type problem by using
gsutil -m -h "Content-Type:image/jpeg" cp -r . gs://mybucket.appspot.com
when transferring the images. But the problem isn't solved, the images continue to be downloaded automatically when accessed with the direct link.

Related

Get TinyMCE 4.3 Image Tools to save images as regular image files

The new TinyMCE 4.3 Image Tools (eg when cropping) saves images as blob data instead of image files in a directory. the edited mage url is like
blob:http://www.example.com/f2953aa1-e64f-49e1-a6e3-a283986663bf
I want to upload the image file to a specific folder and then use it as regular image referance / path.
Note
The question I am going to put is similar to Image edit issue. but the answer to this question is not working. I also tried http://archive.tinymce.com/forum/viewtopic.php?id=35740 solution but not working because it produces always same name image name.
The basic process is that TinyMCE will create a separate HTTP POST for each image that you modify with the image editor. It will send that image to a URL of your choosing (via HTTP POST) based on the setting of the images_upload_url option in your init.
The image handler at the URL referenced in the images_upload_url (which you have to create) has to do whatever needs to be done to "store" the image in your application. That could mean something like:
Store the item in a folder on your web server
Store the item in a database
Store the item in an asset management system
...regardless of where you choose to store the image your image handler needs to return a single line of JSON telling TinyMCE the new location of the image. As referenced in the TinyMCE documentation this might look like:
{ location : '/uploaded/image/path/image.png' }
TinyMCE will then update the image's src attribute to the value you return. If you use the images_upload_base_path setting in the init that will be prepended to the returned location. The TinyMCE page has more details on all of this:
https://www.tinymce.com/docs/advanced/handle-async-image-uploads/
The net here is that TinyMCE knows when an embedded image exists in your content but it can't possibly know what to do with that image in the context of your application so that job (the "image handler") is something you must create.

Open PDF in browser instead of downloading it

After uploading a PDF to the Media Archive, I am trying to link to it from a page on a site.
While editing content, I use the hyperlink tool then select the PDF I want to link to via the URL input box.
After saving and publishing the content, clicking the link downloads the PDF and I don't see any apparent way to make this view-able in the browser by using the current Media ID Composite provides. When rendered, we get this:
pdf
Is there a way that I can reference a PDF without using the Media ID and simply use the file name instead?
Here is the Request/Response header info:
After reading what Pauli Østerø said, I understand the problem but am still not able to think of a solution.
I can get the PDF to view in the browser by adding ?download=false to the href URL via Developer Tools. But when I try to add ?download=false to the href through Composite, it doesn't take affect and I get the console output: "Resource interpreted as Document but transferred with MIME type application/pdf: "http://c1.wittenauers.com/media/4afb7bc8-f703-469d-a9b2-a524d8f93dcb/ryc7iw/CompositeDocumentation.PDF"."
Here is the network trace that was asked for by Pauli. In the image, I included the bit where I add ?download=false to the URL, in source view, just in case there could be another way to add it.
Edit: URL and headers for the page.
Here is the link to the page that contains the link:
http://c1.wittenauers.com/cafe/test
Here is the headers for the page containing the link:
From what you're experiencing, it seems to me that Composite have gotten the MIME type of your uploaded file wrong, and is therefor not correctly telling the browser that this file is a pdf, and the browser doesn't know what to do with it.
Try deleting the file and uploading it again.
Try add ?download=false and the end of the href to the file. You prob. need to go into source mode of the content editor.
This is the exact line in the Source Code which is responsible for this behavior, and the logic is as follows
If there is no Querystring named download, the attachment is determined by the Mime Type. Only png, gif and jpeg will be shown inline, the rest will be shown as attachment.
If there is a Querystring named download with a value of false, it will override the Mime Type check and always force the Content-Disposition to be inline.
I made a quick test here to show that the behaviour is a expected. At least in my Chrome browser in Windows 8
Force download: https://www.dokument24.dk/media/9fdd29da-dde8-41f7-ba4c-1117059fdf06/z8srMQ/test/Prisblad%202015%20inkl%20moms.pdf
Show in browser: https://www.dokument24.dk/media/9fdd29da-dde8-41f7-ba4c-1117059fdf06/z8srMQ/test/Prisblad%202015%20inkl%20moms.pdf?download=false
Expanding on Pauli's answer, you can add the following snippet to your page template to automatically add the '?download=false' to all pdf links.
$("a").each(function () {
if (this.href.includes(".pdf")) {
this.href = this.href + "?download=false";
}
})

Getting Titles of images while using WGET

I am downloading images using wget for a learning project which i am succcesfully able to do. However, some sites have all images titled with the same name - in such cases, is it possible for wget to read the title of the images in the page and output the images with the title name ?
For example if all the images would by default be saved as 1.jpg I would like them to be saved as .jpg where ImageTitle is derived from the Title of the image.
Thanks.

get screenshot from video using ffmpeg

I'm building a Wordpress site that allow admin to upload and attach a video clip to a post. After the video was uploaded successfully, I use ffmpeg to get a screenshot from that video, and that screenshot would be the post featured image.
In my case, the screenshot was generated successfully. In next step, I use Wordpress function media_sideload_image() to copy that screenshot to WP 'uploads' folder and make that image as post featured image.
But for some reasons, the media_sideload_image() returned a WP_Error: http_404. I have checked the url of the temporary screenshot image, but it was valid and display a screenshot nicely.
In this case, I guess that the media_sideload_image() was called after the ffmpeg command, but at that time, the ffmpeg process has not completely finished so the media_sideload_image() encountered http_404 error.
So the question is: Is there any way to make sure the screenshot was saved completely before running media_sideload_image() function after that, to avoid http_404 error ?
Thank you very much !
I am not sure if its even possible to check whether the file was completely saved or not. But you can check if the image file exists.
if (file_exists($file)) {
media_sideload_image($file, $post_id, $desc);
}
It should solve the problem but if for some reason it takes longer to fully save the file, you can add a delay for a few seconds.
sleep(2); // 2 Second delay before checking and saving the file.
if (file_exists($file)) {
media_sideload_image($file, $post_id, $desc);
}

Test Local Background Image on Live Site with Chrome Dev Tools?

Is there any way to use Chrome dev tools to test different images? I have created a new background graphic and I would like to test it on a live site that already has a background graphic on the <body> tag. I don't want to change the live site yet, though. Just test it to see what the new image looks like. Is this possible?
Here is the answer, courtesy of Rob Donovan # superuser (via https://superuser.com/a/839500/454034)
Since you mentioned 'Chrome', you could use Chrome extensions to do this, to enable local access to your files.
Follow these steps:
1) In the local folder where your image(s) are, create this file called 'manifest.json' and enter this:
{
"name": "File Exposer",
"manifest_version": 2,
"version": "1.0",
"web_accessible_resources": ["*.jpg","*.JPG"]
}
2) Put this is your chrome address bar: chrome://extensions/
3) Make sure 'Developer mode' is checked (Top right of page)
4) Click the 'Load Unpacked Extension' button
5) Navigate to the local folder where the image(s) and the manifest.json file is, click ok
6) The extension 'File Exposer' should now be listed in the list, and have a check mark against 'Enabled'. If the folder is on a network drive or other slow drive or has lots of files in, it could take 10-20 seconds or more to appear in the list.
7) Note the 'ID' string that has been associated with your extension. This is the EXTENSION_ID
8) Now in your HTML you can access the file with the following, changing the 'EXTENSION_ID' to whatever ID your extension generated:
<img src='chrome-extension://EXTENSION_ID/example1.jpg'>
Note, that the *.jpg is recursive and it will automatically match files in the specified folder and all sub folders, you dont need to specify for each sub folder. Also note, its Case Sensitive.
In the 'img' tag you don't specify the original folder, its relative from that folder, so only sub folders need to be specified.
If you modify the manifest.json file, you will need to click the 'Reload (Ctrl+R)' link next to the extension.
Another option would be to start a simple http server.
In your terminal (or command prompt), cd into the directory where your images are saved and then type python -m SimpleHTTPServer for Python 2.
For Python 3 use the following command: python -m http.server [<portNo>]
Now you can reference the images in dev tools using http://localhost:8000/filename.jpg.
This is not an exact answer to your question, but one way you could do it is to use something like dropbox public folder. Once the image is in the folder you can just right click and copy a public url to use in the dev tools.
I think the best and simplest solution is to convert your image into Base64 (you can use any online/offline tool for that) and then just paste the output inside DevTools.
If you need it in an IMG tag, do it like that:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
If you need it as a background image, you can do it like that:
.background {
background-image: url("data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==");
}
My, what a difference six years makes! Found this drop-dead-simple method at https://umaar.com/dev-tips/174-drag-drop-image-local-overrides/
Open the Chrome inspector preferences.
Check "Enable Local Overrides."
Locate the original image in the html.
Right-click the file path, and choose "Locate in Sources Panel."
With the image selected in the left panel of the Sources panel, drag
the replacement image from your desktop and onto the preview panel
on the right.
As you hover, you'll see a dotted outline with the text "Drop image
file here." Drop image file there. ;-)
You may need to refresh the page for the new image to display. Yes,
it will persist as long as the Inspector is open and "Local
Overrides" is enabled.
NOTE: The file name will not change in the browser, but the new image will display where the original one displayed previously.
You can replace the url of the background image in the Elements panel with the url of the image you wish to try. Check this link to see how that is done.
This change will show effect immediately in your browser window. As Johan Linder mentioned, you will have to host the image somewhere in case you have the image on your computer.
nate wilton's answer correctly points out how to do this using a Chrome extension.

Resources