Resize images by passing values through the url

Asked

Viewed 647 times

2

On my software download site, there are images of various sizes, for example: for each program there is an image (thumbnail), a large image and screenshots.

Only I want to make the images all the same size and the url pass the size I want of the image. Something like: uploads/imagens/imagen-teste.jpg?w=500&h=500. If I want the same image in another size, I would only pass the value by the url, so: uploads/imagens/imagen-teste.jpg?w=100&h=100.

How can I do this? Or is there a plugin that does this?

  • if you already have the script, and use a function that accepts external parameters, you can call the function as follows on your php &#Xa page;function alteraDimensoes(width, height) would look like this: function alteraDimensoes(<?= img["width"] ?>,<?= img["height"] ?>) But remember to use GET

1 answer

3

Hello.

To go through the URL, you would need to request a get by passing width and height.

You can do this easily with PHP.

<?php

//Parametros obtidos via Get
$width = $_GET('WIDTH');
$height = $_GET('HEIGHT');

//URL da imagem
$url = 'img/foto.jpg';

//pega a imagem para redimensionar com base na URL
$image = imagecreatefromjpeg($url);

//pega a largura e altura original da imagem
$orig_width = imagesx($image);
$orig_height = imagesy($image);

//Gera a nova imagem e redimensiona com base nos valores passados
$new_image = imagecreatetruecolor($width, $height);
imagecopyresized($new_image, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);

//Exibe a imagem
imagejpeg($new_image);
?>

for more information: https://blog.countableset.com/2012/01/03/gd-slash-php-image-resize-based-on-width/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.