Javascript ile resim boyutlandırmak için bir kod aramaktaydım. öyle ki resmin genişliği veya yüksekliği farklı ise istediğim maksimum genişlik ve yüksekliğe ölçeklemesini istiyordum. bunun için şurada güzel bir kod buldum. umarım işinize yarar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function ResizeImage(image, maxwidth, maxheight) { if (image.className == "Thumbnail") { w = image.width; h = image.height; if( w == 0 || h == 0 ) { image.width = maxwidth; image.height = maxheight; } else if (w > h) { if (w > maxwidth) image.width = maxwidth; } else { if (h > maxheight) image.height = maxheight; } image.className = "ScaledThumbnail"; } } |
bu kodu <head> </head> tagları arasına alacaksınız
örnek kullanımı da şöyle
< img src=”resim.jpg” class=”Thumbnail” onload=”ResizeImage(this, 200, 250)” />
200 maksimum genişlik, 250 ise maksimum yüksekliği gösterir onları isteğinize göre değiştirebilirsiniz
eğer resim önceden başka bir class adına sahipse, mesela resimclassı diye, class adı şu şekilde olacak
class=”resimclassı Thumbnail”