@
@
@Twitt2017-07-10 15:09:49
Yii
@Twitt, 2017-07-10 15:09:49

How to display a cropped photo in Yii2?

Good afternoon!
I need to upload a photo, and on one of the pages all the photos must be the same size, and when you click on one of the photos on another page, it already opens in its original size. Because users upload images of various sizes, now they are displayed either huge or small, and the result is not what is needed.
It is necessary to make sure that when uploading photos to the server it is like this:
either two versions of the photo are loaded (original and crop) (accordingly, make fields in the database besides one - photo, divide into original and crop),
or you need to load one version of the photo, and somehow in the view itself, use some Crop method so that all the photos that will be on the page are of the same cropped size (for example, 640x640). What is the best way to do it, and with what extension? Tried with Imagine

Image::crop(Yii::getAlias("@webroot") . '/images/2ab17480c5807726bdab3edd388e1ce0.jpg', 640, 640)
->save(Yii::getAlias('@webroot') . '/images/crop- photo.jpg');

But here it crops the photo based on the photo that already exists on the server.
And I will be very grateful if you tell me which method is better (separation into original and crop, or the second where in the view we would call the crop method).
Thank you )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-07-10
_

they do it this way:
cropping is usually done when uploading, sometimes they do it when a photo is requested, but there are a lot of pitfalls, so I advise it when uploading.
you only need to store the name of the photo in the database, but the thumbnails are simply scattered in folders. There are many folder structure options, here is the first example:

echo '/images/originals/'.$model->imagename;  //оригиналы
echo '/images/small/'.$model->imagename;  //миниатюры

more flexible option:
echo '/images/2017/07/original/'.$model->imagename;  //оригиналы
echo '/images/2017/07/400x400/'.$model->imagename;  //миниатюры 400x400
echo '/images/2017/07/1000x/'.$model->imagename;  //миниатюры 1000xauto
echo '/images/2017/07/x50/'.$model->imagename;  //миниатюры autox50

accordingly, you can create a method in the model and use something like this:
echo $model->getImageUrl('400x');

D
Dmitry, 2017-07-10
@slo_nik

Good afternoon.
I would not crop the photo in the view, why the extra movement?
When uploading, make two images, in the table one (!) field for the photo name (the main part of the name, for example image.jpg), and save it to a directory with the names original_image.jpg and crop_image.jpg.
When you display a photo in the view, substitute either original or crop for the name to display either the original or the cropped photo.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question