Skip to content

Instantly share code, notes, and snippets.

View edutrul's full-sized avatar
💭
Changing the world

Eduardo Telaya edutrul

💭
Changing the world
View GitHub Profile
@edutrul
edutrul / convert-integer-to-roman.php
Created September 8, 2017 16:26
How to convert integer to Roman number
<?php
/**
* Converts integer to Roman.
*
* @param int $integer
* Integer to be converted to Roman string.
*
* @return string
*/
public function integerToRoman($integer) {
@edutrul
edutrul / from_byte_array_image_to_display_browser.php
Last active February 15, 2023 18:37
PHP Convert byte array to php image and display to browser
<?php
// https://example.com/api/v1/file?id=8972893948981
// Displays the following json object:
// {"asset":{"file":{"data":[-119,80,78,71,13,10,26,10,0,0,0,13,73,,,,, etc
// Here's the php cone snippet to display to image in the browser.
$result = json_decode(file_get_contents($full_url));
$raw_png = implode(array_map('chr', $result->asset->file->data));
header('Content-Type: image/png');
print $raw_png;
die ($raw_png);
@edutrul
edutrul / flag_game.html
Created February 5, 2023 02:45
Guess country name from 4 choices -- game generated by chat gpt3
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Country Image Guess Game</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="game">
<img id="flag" src="" alt="Flag">
@edutrul
edutrul / index.html
Created January 25, 2023 01:50
CHAT GPT 3 Product page listing automatically
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<title>Product Page</title>
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-6">
@edutrul
edutrul / download-a-file-in-drupal9-drupal8.php
Last active December 2, 2022 12:48
How to download a file programmatically using guzzle in Drupal 8 / Drupal 9
<?php
$external_file = 'https://www.example.com/test.png'
$destination = 'sites/default/files/a-directory/test.png';
/** @var GuzzleHttp\Psr\Response $response */
$response = \Drupal::httpClient()->get($external_file, ['sink' => $destination]);
// file gets downloaded under /sites/default/files/a-directory/test.png
@edutrul
edutrul / github-oauth2-client.php
Last active September 7, 2022 02:47 — forked from asika32764/github-oauth2-client.php
Simple PHP example of using Github's OAuth 2 API
<?php
// Code taken from https://gist.github.com/asika32764/b204ff4799d577fd4eef
// Code changes by @edutrul "added https" in all responses
// Makes sure when creating the app you deploy to a hosting/cloud
// If you want to do it quickly then use heroku.
// When adding the app in github make sure you just add the domain name like https://example.com
// Notice the "https" being added in the callback, website and other fields in github page.
define('OAUTH2_CLIENT_ID', '<YOUR CLIENT ID>');
@edutrul
edutrul / mymodule--feature.js.js
Last active August 12, 2022 19:11
How to add javascript by using drupal 8 libraries using Drupalsettings(to pass php(backend) variables to js(frontend). This can be applied to files: ".theme", ".module"m "field formatter class", "views field handler", etc files. Also uses javascript drupal behaviours (great practice in D8) Plus contains "once" which only enables to execute one t…
// File js/mymodule--feature.js
(function ($) {
Drupal.behaviors.mymodule__feature = {
attach: function (context, drupalSettings) {
// Use once to avoid tu call multiple times a js.
$(document).once('feature').on('click', '.button--feature', (function (event) {
event.preventDefault();
if (typeof drupalSettings.mymodule.feature.id != 'undefined') {
console.log('Your great logic goes here and will NOT be called multiple times');
}
@edutrul
edutrul / MyModuleMenuLink.php
Created May 15, 2020 01:26
Menu item title dynamically based on user role.
@edutrul
edutrul / _readme.md
Created April 27, 2020 00:02 — forked from gka/_readme.md
PHP Endpoint for Github Webhook URLs

PHP Endpoint for Github Webhook URLs

If you love deploying websites using Github, but for some reason want to use your own server, this script might be exactly what you need.

  1. Put github.php somewhere on your PHP-enabled web server, and make it accessible for the outside world. Let's say for now the script lives on http://example.com/github.php
@edutrul
edutrul / README.md
Created April 26, 2020 19:54
Troubleshoot git webhook in Drupal 8

Make sure:

  • web/github/github.php has correct permissions

In github webhooks make sure you add domain